Whats wrong with this code?
DataSet ds = new DataSet();
DataTable tbl = new DataTable("Tbl");
tbl.Columns.Add("field1", typeof(string));
tbl.Columns.Add("field2", typeof(string));
tbl.Columns.Add("field3", typeof(int));
ds.Tables.Add(tbl);
DataTable tbl2 = new DataTable("Tbl2");
tbl2.Columns.Add("field1", typeof(string));
tbl2.Columns.Add("field2", typeof(string));
tbl2.Columns.Add("field3", typeof(int));
ds.Tables.Add(tbl2);
dataGrid1.DataSource = ds;
dataGrid1.DataMember = tbl.TableName;
DataGridTableStyle ts = new DataGridTableStyle();
ts.MappingName = tbl.TableName;
dataGrid1.TableStyles.Add(ts);
dataGrid1.TableStyles[tbl.TableName].GridColumnStyles["field3"].Width=0;
ts = new DataGridTableStyle();
ts.MappingName = tbl2.TableName;
dataGrid1.TableStyles.Add(ts);
dataGrid1.TableStyles[tbl2.TableName].GridColumnStyles["field2"].Width=0;
This last line causes an "Object reference not set to an instance of an
object" error.
If I change the DataMember to tbl2 before adding the last TableStyle, no
error.
Apparently just adding a TableStyle to the TableStyles collection doesn't
automatically generate a complete set of default ColumnStyles as the
documentation states. What exactly is the rule here?