1
votes

I've a DataTable and have two columns 'FirstName' and 'LastName'.

I've created a datacolumn to concat value from these two columns. Here is my code -

DataColumn fullname = dt.Columns.Add("Full Name");
string fn = string.Format(string.Concat("'First Name - '+", "{0}", "+ ';<br />"), "FN");
string ln = string.Format(string.Concat("Last Name - '+", "{0}", "+ ';<br />"), "LN");
otherDetails.Expression = string.Concat(fn, ln);

The code is working fine and concatenates values from two fields. But if any of the column value is null then the expression is not working and is returning as empty.

For example for the 5th record is if first name is 'Tim' and the second name is null the I want the value to be displayed as 'Tim'. But instead for that record the value of this expression column is empty.

Any ideas?

2
string.Concat(null, "2") will return "2", not an empty string or a null value. Could you give more "real" code ? - Raphaël Althaus
This is the real code. The actual concatenation is at the table level. The expression 'First Name' + FN is not returning "First Name - ". Instead it is returning empty. If the column FN is having NULL value for the record. - NLV
Which string constants? FN and LN are the column names. - NLV
Yes I guess you have misunderstood the column expression :). For the column expression we just need to give the column name in the expression. During databinding it uses the column name to get the cell value. Please check how to write column expressions msdn.microsoft.com/en-us/library/… - NLV
oh, I ain't lost my day then ;) - Raphaël Althaus

2 Answers

1
votes

Think you may try

string fn = string.Format(string.Concat("'First Name - '+", "{0}", "+ ';<br />"), "IsNull(FN, '')");
string ln = string.Format(string.Concat("Last Name - '+", "{0}", "+ ';<br />"), "IsNull(LN, '')";
1
votes

Try this syntax for the string instantiation:

string  ln = string.Format(string.Concat("Last Name - '+", "{0}", "+ ';<br />"), "LN") ?? "";

This should set any nulls to the empty string.