Calculated column: No Expression result

K

Karsten Strobel

Hello List!

I have an untyped dataset with a very simple table, two columns A and B and
one extra column ColumnCalc with the Expression "A+B". Details see code
snippet below.

When I bind the table to a data grid, I can enter values for A and B. Also
ColumnCalc is displayed as a read-only column. But I never see the result of
the A+B expression displayed in ColumnCalc. The value of ColumnCalc is
always <null>, even if both A and B have a value.

What am I doing wrong?

Thanks
Karsten

// dataSet1

this.dataSet1.DataSetName = "NewDataSet";

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});

this.dataTable1.TableName = "Table1";

// dataColumn1

this.dataColumn1.ColumnName = "A";

this.dataColumn1.DataType = typeof(int);

// dataColumn2

this.dataColumn2.ColumnName = "B";

this.dataColumn2.DataType = typeof(int);

// dataColumn3

this.dataColumn3.ColumnName = "ColumnCalc";

this.dataColumn3.DataType = typeof(int);

this.dataColumn3.Expression = "A+B";

this.dataColumn3.ReadOnly = true;
 
W

William Ryan eMVP

Something else is probably happening, can you post the rest of the code...

I just whipped up the following and ti works without a glitch:
da.Fill(ds, "MyTable")

Dim dc As New DataColumn("Expression", System.Type.GetType("System.Decimal"))

ds.Tables("MyTable").Columns("ItemNo").ColumnName = "A"

ds.Tables("MyTable").Columns("ItemCost").ColumnName = "B"

ds.Tables("MyTable").Columns("A").DataType = GetType(System.Int32)

ds.Tables("MyTable").Columns("B").DataType = GetType(System.Int32)

dc.Expression = "(A) + (B)"

dc.ReadOnly = True

ds.Tables("MyTable").Columns.Add(dc)

dg.DataSource = ds.Tables(0)



However, depending on the datatypes of the underlying columns, I can get some weird behavior. For instance, If I change either A or B to varchar, it'll append them to each other which isn't really that weird. I'm wondering if you aren't having a similar problem.




--

W.G. Ryan, eMVP

http://forums.devbuzz.com/
http://www.knowdotnet.com/williamryan.html
http://www.msmvps.com/WilliamRyan/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top