changing datagrid column width

  • Thread starter Thread starter Amos
  • Start date Start date
A

Amos

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
 
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/default.asp?url=/library/en-us/cpref/html/
frlrfsystemwindowsformsdatagridtablestyleclasstopic.asp for more details on
the DataGridTableStyle class and its properties.

hth

-Joel
--------------------
 
Back
Top