Expression column is Null?

B

Bill Todd

I have a DataTable in a DataSet. The ColumnNames of two of the
DataColumns are First_Name and Last_Name. I have added another
DataColumn and set the Expression property to:

First_Name + ' ' + Last_Name

However, in my WinForms DataGrid the expression column always displays
(Null) even though both First_Name and Last_Name have values. What am
I doing wrong?

Thanks,
 
L

\(Laurent Jordi\)

Hi..

The last before I sleep... It's monday here...

You have tu use a culumn template to use expressions in your column...

See help for that, it's clear enougth...

Regards...

LJ

www.eztree-msdn.com
 
R

Ravi[MSFT]

Not sure what exactly you are doing wrong. But that should work.

Check out the sample below.
----
using System;
using System.Data;

class Test {
public static void Main()
{
try {
DataSet ds = new DataSet();
DataTable dtCust = ds.Tables.Add("Customers");
DataColumn c = dtCust.Columns.Add("CustId", typeof(int));
dtCust.PrimaryKey = new DataColumn[] { c };
dtCust.Columns.Add("FirstName", typeof(string));
dtCust.Columns.Add("LastName", typeof(string));
DataColumn cExp = dtCust.Columns.Add("FullName",
typeof(string));
cExp.Expression = "FirstName + ' ' + LastName";
dtCust.Columns.Add("Age", typeof(int));

dtCust.Rows.Add(new object[] { 1, "John", "Doe", null, 25 });

ds.WriteXml(Console.Out);
} catch (Exception e) {
Console.WriteLine(e);
}
}
}
 
W

William Ryan eMVP

Bill:

That looks good and I use the same expression quite a bit. The only thing
that comes to mind is that if either of the fields is null, the whole thing
will be. However, here's a code snippet I use all the time and it works
fine... Is this essentially what you are doing (I'm guessing that you aren't
adding each column individually you are getting them from the DB, but that
shouldn't make any difference)

DataTable dtEmployees = new DataTable();
DataColumn dcFullName = new DataColumn();
DataColumn dcFirstName = new DataColumn();
DataColumn dcLastName = new DataColumn();
dcFullName.DataType = System.Type.GetType("System.String");
dcFirstName.DataType = System.Type.GetType("System.String");
dcLastName.DataType = System.Type.GetType("System.String");

dcFullName.ColumnName = "FullName";
dcFirstName.ColumnName = "First_Name";
dcLastName.ColumnName = "Last_Name";
dtEmployees.Columns.Add(dcFullName);
dtEmployees.Columns.Add(dcFirstName);
dtEmployees.Columns.Add(dcLastName);
dcFullName.Expression = "Last_Name + ', ' + First_Name";
DataRow dro = dtEmployees.NewRow();
dtEmployees.Rows.Add(dro);
dro[1] = "William";
dro[2] = "Ryan";
MessageBox.Show(dro[0].ToString()); //Displays Ryan, William
 
B

Bill Todd

Your code appears to do exactly what I have done at design time using
the Columns Collection Editor. I'll play with it and see if I can find
anything. At the moment I am stumped. Thanks.
 
W

William Ryan eMVP

Laurent:

I don't think you need to use any of those to get an expression column to
work properly, but I'm interested in seeing the approach you are referring
to. could you post some more info on it?
 
B

Bill Todd

The problem is that the Columns Collection Editor is buggy. Adding the
calculated column at design time in an untyped DataSet never works.
Adding it at runtime always works.
 
K

Kevin Yu [MSFT]

Hi Bill,

I have checked it. However, I cannot reproduce it. The FullName column only
appears to be null when either of the First_Name column or Last_Name column
is null. If the problem still persists, for workarounds, please try to add
the expression at runtime.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 

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