How to change Datagrid column width???
-
I'm trying this code, but I get the error :" Object reference not set to an instance of an object". The code: // Create new Table Style DataGridTableStyle ts = new DataGridTableStyle(); ts.MappingName = "Employees"; this.dataGrid1.TableStyles.Clear(); this.dataGrid1.TableStyles.Add(ts); // Assign New Width to DataGrid column this.dataGrid1.TableStyles["Employees"].GridColumnStyles["EmployeeName"].Width = newwidth; I set the DataSource for the Datagrid to dataset.Employees on the DataGrid Properties. Does anyone know what I'm doing wrong or what I could try??
-
I'm trying this code, but I get the error :" Object reference not set to an instance of an object". The code: // Create new Table Style DataGridTableStyle ts = new DataGridTableStyle(); ts.MappingName = "Employees"; this.dataGrid1.TableStyles.Clear(); this.dataGrid1.TableStyles.Add(ts); // Assign New Width to DataGrid column this.dataGrid1.TableStyles["Employees"].GridColumnStyles["EmployeeName"].Width = newwidth; I set the DataSource for the Datagrid to dataset.Employees on the DataGrid Properties. Does anyone know what I'm doing wrong or what I could try??
You actually have to add a new
DataGridColumnStyle
before you can refer to it. If you step through your code in the debugger, you'll notice that.GridColumnStyles["EmployeeName"]
name isnull
- you never created such a column. You have to create all theDataGridColumnStyle
s you want mapped, theDataGridTableStyle
, add the columns to the table style'sGridColumnStyles
collection property, then add the table to theDataGrid
'sTableStyles
collection property.Microsoft MVP, Visual C# My Articles