It's not difficult, but your problem is that you need to name your DataTable. The DataGrid associates a particular DataGridTableStyle with a DataGrid by matching the DataTable.TableName (you can pass this to the constructor) with one of the DataGridTableStyle's MappingName that was added to the DataGrid.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 the DataGridTableStyle.MappingName property documentation. Also, why are you binding a DataGridBooleanColumn to an int 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