masdotnet
Jan 4th, 2005, 06:20 PM
I'm trying to use the .Width property of a DataGridColumnStyle to change the width, but the width will only go to the Datagrid PreferredColumnWidth. Does anyone know what properties to set or code that will change the column widths individually?
Aaron Young
Jan 24th, 2005, 06:05 PM
Are you using the DataGridColumnStyle class appropriately?
Example: // Create an example DataTable called "Example"
DataTable table = new DataTable("Example");
// Add a few String fields
table.Columns.Add("Column1", typeof(string));
table.Columns.Add("Column2", typeof(string));
table.Columns.Add("Column3", typeof(string));
table.Columns.Add("Column4", typeof(string));
table.Columns.Add("Column5", typeof(string));
// Add a few rows of example data
table.Rows.Add(new object[] { "Value 1", "Value 2", "Value 3", "Value 4", "Value 5" });
table.Rows.Add(new object[] { "Value 1", "Value 2", "Value 3", "Value 4", "Value 5" });
table.Rows.Add(new object[] { "Value 1", "Value 2", "Value 3", "Value 4", "Value 5" });
table.Rows.Add(new object[] { "Value 1", "Value 2", "Value 3", "Value 4", "Value 5" });
table.Rows.Add(new object[] { "Value 1", "Value 2", "Value 3", "Value 4", "Value 5" });
// Bind the "Example" table to the DataGrid
dataGrid1.DataSource = table;
// Create a new DataGrid TableStyle
DataGridTableStyle style = new DataGridTableStyle();
// Map the TableStyle to the "Example" table
style.MappingName = "Example";
// Add the TableStyle to the Grid, at which point it creates the
// Individual ColumnStyles for each column in the table
dataGrid1.TableStyles.Add(style);
// Alter the width of a specific column/columns
style.GridColumnStyles["Column1"].Width = 10;
style.GridColumnStyles["Column3"].Width = 150;