DataGrid Set Column Width
-
I want to know how to use DataGridColumnStyle to set the width of my datagrid. I know how to use PreferredColumnWidth to set the width of the oveall colunm, but it is not what I want. I want to set individual column. Here is my program I also want to know if there is a good documentation for the .net classes. While I was working in the DataGridClass. My class is defined as dataGrid1, from dataGrid1, there is not way to know that DataGridColumnStyle exists. I want to know is there is a good book or documentation tha point out intaction between associated classes like for instance DataGrid and DataGridColumnStyle.
private void Form1_Load(object sender, System.EventArgs e) { double[] dData1 = new double[100]; double[] dData2 = new double[100]; DataTable dt = new DataTable(); DataRow dr; dt.Columns.Add(new DataColumn("Index", typeof(int))); dt.Columns.Add(new DataColumn("Sinve Values", typeof(float))); dt.Columns.Add(new DataColumn("Cosine Values", typeof(float))); dataGrid1.PreferredColumnWidth = 100;//set the overall column width for(int i=0;i<100;i++) { dData1[i] = Math.Sin(0.1*i); dData2[i] = Math.Cos(0.2*i); dr = dt.NewRow(); dt.Rows.Add(dr); dr[0] = i;; dr[1] = dData1[i]; dr[2] = dData2[i]; } dataGrid1.DataSource = new DataView(dt); waveformPlot1.PlotY(dData1); waveformPlot2.PlotY(dData2); }
-
I want to know how to use DataGridColumnStyle to set the width of my datagrid. I know how to use PreferredColumnWidth to set the width of the oveall colunm, but it is not what I want. I want to set individual column. Here is my program I also want to know if there is a good documentation for the .net classes. While I was working in the DataGridClass. My class is defined as dataGrid1, from dataGrid1, there is not way to know that DataGridColumnStyle exists. I want to know is there is a good book or documentation tha point out intaction between associated classes like for instance DataGrid and DataGridColumnStyle.
private void Form1_Load(object sender, System.EventArgs e) { double[] dData1 = new double[100]; double[] dData2 = new double[100]; DataTable dt = new DataTable(); DataRow dr; dt.Columns.Add(new DataColumn("Index", typeof(int))); dt.Columns.Add(new DataColumn("Sinve Values", typeof(float))); dt.Columns.Add(new DataColumn("Cosine Values", typeof(float))); dataGrid1.PreferredColumnWidth = 100;//set the overall column width for(int i=0;i<100;i++) { dData1[i] = Math.Sin(0.1*i); dData2[i] = Math.Cos(0.2*i); dr = dt.NewRow(); dt.Rows.Add(dr); dr[0] = i;; dr[1] = dData1[i]; dr[2] = dData2[i]; } dataGrid1.DataSource = new DataView(dt); waveformPlot1.PlotY(dData1); waveformPlot2.PlotY(dData2); }
If you want to know if there's good documentation, then read it and determine whether you think it's good. You can set the width of a specific column using a
DataGridColumnStyle
that you add to aDataGridTableStyle
for a specificDataTable
(or type, if reflecting against something other than aDataSet
orDataTable
), which you add toDataGrid.TableStyles
. You should read the documentation, which also presents articles. TheDataGridColumnStyle
does exactly what you're asking and is specific to the column it represents in aDataGrid
. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog] -
If you want to know if there's good documentation, then read it and determine whether you think it's good. You can set the width of a specific column using a
DataGridColumnStyle
that you add to aDataGridTableStyle
for a specificDataTable
(or type, if reflecting against something other than aDataSet
orDataTable
), which you add toDataGrid.TableStyles
. You should read the documentation, which also presents articles. TheDataGridColumnStyle
does exactly what you're asking and is specific to the column it represents in aDataGrid
. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]After adding all these extra codes, it still doesn't work. It shouldn't take all too much effort jsut to increse or decrease the length of a column in a grid. In terms of the documentation, I think it would have been bette to show interaction from one class to another.
private void Form1_Load(object sender, System.EventArgs e) { double[] dData1 = new double[100]; double[] dData2 = new double[100]; DataTable dt = new DataTable(); DataRow dr; DataGridTableStyle ts1 = new DataGridTableStyle(); DataGridColumnStyle boolCol = new DataGridBoolColumn(); dt.Columns.Add(new DataColumn("Index", typeof(int))); dt.Columns.Add(new DataColumn("Sinve Values", typeof(float))); dt.Columns.Add(new DataColumn("Cosine Values", typeof(float))); ts1.MappingName = "Index"; boolCol.MappingName = "Index"; boolCol.Width = 20; ts1.GridColumnStyles.Add(boolCol); dataGrid1.TableStyles.Add(ts1); for(int i=0;i<100;i++) { dData1[i] = Math.Sin(0.1*i); dData2[i] = Math.Cos(0.2*i); dr = dt.NewRow(); dt.Rows.Add(dr); dr[0] = i;; dr[1] = dData1[i]; dr[2] = dData2[i]; } dataGrid1.DataSource = new DataView(dt); waveformPlot1.PlotY(dData1); waveformPlot2.PlotY(dData2); }
-
After adding all these extra codes, it still doesn't work. It shouldn't take all too much effort jsut to increse or decrease the length of a column in a grid. In terms of the documentation, I think it would have been bette to show interaction from one class to another.
private void Form1_Load(object sender, System.EventArgs e) { double[] dData1 = new double[100]; double[] dData2 = new double[100]; DataTable dt = new DataTable(); DataRow dr; DataGridTableStyle ts1 = new DataGridTableStyle(); DataGridColumnStyle boolCol = new DataGridBoolColumn(); dt.Columns.Add(new DataColumn("Index", typeof(int))); dt.Columns.Add(new DataColumn("Sinve Values", typeof(float))); dt.Columns.Add(new DataColumn("Cosine Values", typeof(float))); ts1.MappingName = "Index"; boolCol.MappingName = "Index"; boolCol.Width = 20; ts1.GridColumnStyles.Add(boolCol); dataGrid1.TableStyles.Add(ts1); for(int i=0;i<100;i++) { dData1[i] = Math.Sin(0.1*i); dData2[i] = Math.Cos(0.2*i); dr = dt.NewRow(); dt.Rows.Add(dr); dr[0] = i;; dr[1] = dData1[i]; dr[2] = dData2[i]; } dataGrid1.DataSource = new DataView(dt); waveformPlot1.PlotY(dData1); waveformPlot2.PlotY(dData2); }
It's not difficult, but your problem is that you need to name your
DataTable
. TheDataGrid
associates a particularDataGridTableStyle
with aDataGrid
by matching theDataTable.TableName
(you can pass this to the constructor) with one of theDataGridTableStyle
'sMappingName
that was added to theDataGrid.TableStyles
property. Your modified code below works fine:double\[\] dData1 = new double\[100\]; double\[\] dData2 = new double\[100\];
DataTable dt = new DataTable("Test");
DataRow dr;
DataGridTableStyle ts1 = new DataGridTableStyle();
ts1.MappingName = dt.TableName;
DataGridColumnStyle boolCol = new DataGridBoolColumn();
dt.Columns.Add("Index", typeof(int));
dt.Columns.Add("Sinve Values", typeof(float));
dt.Columns.Add("Cosine Values", typeof(float));
boolCol.MappingName = "Index";
boolCol.Width = 20;
ts1.GridColumnStyles.Add(boolCol);
dataGrid1.TableStyles.Add(ts1);
for (int i=0; i<100; i++)
{
dData1[i] = Math.Sin(0.1*i);
dData2[i] = Math.Cos(0.2*i);
dr = dt.NewRow();
dt.Rows.Add(dr);
dr[0] = i;
dr[1] = dData1[i];
dr[2] = dData2[i];
}
dataGrid1.DataSource = dt;You did, however, forget to add column styles for your other columns. When you use table styles with defined columns (even just 1), no columns are auto-generated. Besides, using a
DataTable
here is unnecessary. A simple array of some struct like so would work:public struct Data
{
public int Index;
public double Sinve;
public double Cosine;
}In this case, you set
DataGridTableStyle.MappingName
to "Data". This is documented in theDataGridTableStyle.MappingName
property documentation. Also, why are you binding aDataGridBooleanColumn
to anint
DataColumn
? That's going to set it to either 0 or 1 and will start (for all but the first row) in an indeterminate state. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog