473,395 Members | 1,418 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

changing datagrid column width

Can I add a column to the datagrid and also resize it? I add them like this:

DataTableAddress.Columns.Add("Col1", typeof(string));
DataTableAddress.Columns.Add("Col2", typeof(string));
DataTableAddress.Columns.Add("Col3", typeof(string));

I'd like to add to resize them without using a datacolumn because there's
over 30 columns and because the application is working well already (I don't
know how the datacolumn additions will affect the application).

Thanks,
Amos
Nov 16 '05 #1
1 3222
You'll need to create a DataGridTableStyle object and add it to the
TableStyles collection of the data grid. Here's an example that changes
the width of all of the columns.

DataGridTableStyle dgts = new DataGridTableStyle();
dgts.MappingName = "table name"; // replace table name with the name of the
table who's style you want to modify

foreach (DataColumn dc in table.Columns) // where table is an existing
DataTable
{
DataGridColumnStyle dgc = new DataGridTextBoxColumn();

dgc.MappingName = dc.ColumnName;
dgc.HeaderText = dc.ColumnName;
dgc.Width = 25;

dgts.GridColumnStyles.Add(dgc);
}

this.dataGrid1.TableStyles.Add(dgts);

Check out
http://msdn.microsoft.com/library/de...us/cpref/html/
frlrfsystemwindowsformsdatagridtablestyleclasstopi c.asp for more details on
the DataGridTableStyle class and its properties.

hth

-Joel
--------------------
Reply-To: "Amos" <am***@earthlink.net>
From: "Amos" <am***@earthlink.net>
Subject: changing datagrid column width
Date: Mon, 5 Apr 2004 15:14:57 -0400
Lines: 14
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
Message-ID: <uz**************@tk2msftngp13.phx.gbl>
Newsgroups: microsoft.public.dotnet.languages.csharp
NNTP-Posting-Host: 66.50.252.112
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftn gp13.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:235329
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

Can I add a column to the datagrid and also resize it? I add them like this:
DataTableAddress.Columns.Add("Col1", typeof(string));
DataTableAddress.Columns.Add("Col2", typeof(string));
DataTableAddress.Columns.Add("Col3", typeof(string));

I'd like to add to resize them without using a datacolumn because there's
over 30 columns and because the application is working well already (I don'tknow how the datacolumn additions will affect the application).

Thanks,
Amos

--------------------------------------------------------------------
This reply is provided AS IS, without warranty (express or implied).
Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Amos | last post by:
Can I add a column to the datagrid and also resize it? I add them like this: DataTableAddress.Columns.Add("Col1", typeof(string)); DataTableAddress.Columns.Add("Col2", typeof(string));...
9
by: web1110 | last post by:
Hi y'all, I have resized the columns in a DataGrid and I want to set the width of the DataGrid to fit the columns. Just summing the column widths is too short due to the grid and gray row...
5
by: VB Programmer | last post by:
I have a simple datagrid on a webform. (I'm using VB.NET.) In the page load I get some data, set the .DataSource property of the dg, then do a .DataBind. The columns are automatically created. ...
1
by: martin | last post by:
Hi, I have a datagrid that contains 3 colums. This is rendered to the page fine, except that I would like to be able to control the width of each table cell of the datagrid I have the following...
2
by: CSL | last post by:
I am using the DataGrid in a Windows Application, how can I adjust the widths of each column individually.
3
by: Aziz | last post by:
1. I have a shopping basket DataGrid with a list of products. What I want to do is when the user clicks on a row, a button will become visible/be created that allows user to edit the quantity. The...
1
by: Veeves | last post by:
I would like to change the width of a datagrid column at run time. I have noticed when creating a datagrid, if the header text is shorter than the data in the column, then the column width set by...
2
by: cj | last post by:
I was looking over some of my 2003 code today (see below) that loads a foxpro table via oledb connection. I used a sub "autosizecolumns" I found on the web but I never quite understood why they...
2
by: =?Utf-8?B?Y3JlYXZlczA2MjI=?= | last post by:
I have a nested datagrid in a xaml file, the parent datagrid loads the vendor information and the details loads the documents for that vendor in a datagrid. Everything is working fine until I click...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.