By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,114 Members | 1,570 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,114 IT Pros & Developers. It's quick & easy.

changing datagrid column width

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.