Problem with expressions in DataColumn and ISupportInitialize

  • Thread starter Thread starter Sam Dahan
  • Start date Start date
S

Sam Dahan

When I use the Form wizard to add a DataSet, DataTable and connect a
DataGrid to the DataTable, the expression is never evaluated in the
calculated column. Is this a known bug in VS.NET 2003? Is there
aworkaround more convenient than the one I give below?

Thanks for any answers...

Here is the code generated by the wizard (some replaced by ellipsis
for clarity):

((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.dataSet1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.dataTable1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
..
this.dataGrid1.DataSource = this.dataTable1;
..
//
// dataSet1
//
..
this.dataSet1.Tables.AddRange(new System.Data.DataTable[]
{this.dataTable1});
//
// dataTable1
//
this.dataTable1.Columns.AddRange(new System.Data.DataColumn[] {
this.dataColumn1,
this.dataColumn2,
this.dataColumn3});
..
//
// dataColumn1
//
this.dataColumn1.ColumnName = "a";
this.dataColumn1.DataType = typeof(System.Decimal);
//
// dataColumn2
//
this.dataColumn2.ColumnName = "b";
this.dataColumn2.DataType = typeof(System.Decimal);
//
// dataColumn3
//
this.dataColumn3.ColumnName = "ab";
this.dataColumn3.DataType = typeof(System.Decimal);
this.dataColumn3.Expression = "a*b";
this.dataColumn3.ReadOnly = true;
...
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.dataSet1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.dataTable1)).EndInit();
this.ResumeLayout(false);

When I run this and type some value in column a nd b, the value in
column ab is still (null).
However, when I comment out the calls to BeginInit() and EndInit() for
DataTable1, the expression is evaluated as soon as I navigate away
from the column ab, as expected.

------------------------------------
Just for kicks, the following code (implemented in a button click
handler, for example) will generate a datatable that will evaluate the
expression w/o problems:

DataColumn dc1 = new System.Data.DataColumn("A",
typeof(System.Decimal));
DataColumn dc2 = new System.Data.DataColumn("B",
typeof(System.Decimal));
DataColumn dc3 = new System.Data.DataColumn("AB",
typeof(System.Decimal), "A*B");
dc3.ReadOnly = true;

DataTable dt = new System.Data.DataTable("Multiply");
dt.Columns.AddRange(new DataColumn[]{dc1, dc2, dc3});

this.dataGrid1.DataSource = dt;
 
I found a quicker fix, and it consists in moving the
initialization of the expression property of the
DataColumn outside the BeginInit()/EndInit() block.
It means I have to copy that line in the form_load
handler, since the InitializeComponent() is rewritten any
time I use the design view.

I believe that this is a bug in either ADO.NET 2003, or in
the 1.1 framework.
I don't know which team in Microsoft is the one that
should address it. I hope it's fixed in Whidbey!
Sam
-----Original Message-----
When I use the Form wizard to add a DataSet, DataTable and connect a
DataGrid to the DataTable, the expression is never evaluated in the
calculated column. Is this a known bug in VS.NET 2003? Is there
aworkaround more convenient than the one I give below?

Thanks for any answers...

Here is the code generated by the wizard (some replaced by ellipsis
for clarity):

((System.ComponentModel.ISupportInitialize) (this.dataGrid1)).BeginInit();
((System.ComponentModel.ISupportInitialize) (this.dataSet1)).BeginInit();
((System.ComponentModel.ISupportInitialize)
(this.dataTable1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
..
this.dataGrid1.DataSource = this.dataTable1;
..
//
// dataSet1
//
..
this.dataSet1.Tables.AddRange(new System.Data.DataTable[]
{this.dataTable1});
//
// dataTable1
//
this.dataTable1.Columns.AddRange(new System.Data.DataColumn[] {
this.dataColumn1,
this.dataColumn2,
this.dataColumn3});
..
//
// dataColumn1
//
this.dataColumn1.ColumnName = "a";
this.dataColumn1.DataType = typeof(System.Decimal);
//
// dataColumn2
//
this.dataColumn2.ColumnName = "b";
this.dataColumn2.DataType = typeof(System.Decimal);
//
// dataColumn3
//
this.dataColumn3.ColumnName = "ab";
this.dataColumn3.DataType = typeof(System.Decimal);
this.dataColumn3.Expression = "a*b";
this.dataColumn3.ReadOnly = true;
...
((System.ComponentModel.ISupportInitialize) (this.dataGrid1)).EndInit();
((System.ComponentModel.ISupportInitialize) (this.dataSet1)).EndInit();
((System.ComponentModel.ISupportInitialize)
(this.dataTable1)).EndInit();
this.ResumeLayout(false);

When I run this and type some value in column a nd b, the value in
column ab is still (null).
However, when I comment out the calls to BeginInit() and EndInit() for
DataTable1, the expression is evaluated as soon as I navigate away
from the column ab, as expected.

------------------------------------
Just for kicks, the following code (implemented in a button click
handler, for example) will generate a datatable that will evaluate the
expression w/o problems:

DataColumn dc1 = new System.Data.DataColumn("A",
typeof(System.Decimal));
DataColumn dc2 = new System.Data.DataColumn("B",
typeof(System.Decimal));
DataColumn dc3 = new System.Data.DataColumn("AB",
typeof(System.Decimal), "A*B");
dc3.ReadOnly = true;

DataTable dt = new System.Data.DataTable("Multiply");
dt.Columns.AddRange(new DataColumn[]{dc1, dc2, dc3});

this.dataGrid1.DataSource = dt;
 
Back
Top