auto default GridColumnStyles

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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?
 
Russ,
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?

In my experience, the documentation does oversimplify quite a bit on
this point. I ran into similar problems with a grid that wasn't sited
(i.e. BindingContext was nothing). It took quite a while to trace down
the real source of the problem because there is no diagnostic
information (no error message) when the TableStyle is not automatically
populated.

Unfortunately, other than commiseration, I don't have anything concrete
to offer. In my case, I was using a utility subroutine for configuring
my data grids, so I added code to throw an error if the number of
columns in the TableStyle was different than the number in the
DataTable. At least that way I can start out a couple of steps closer
to the root cause when I do have a problem.

-JLS
 
Back
Top