ogrishmania
ogrishmania

Reputation: 29

C# Concatenating two DataTable columns (typeof string) one of which may be null

I have this code:

dt.Columns.Add("denumire", typeof(string));
dt.Columns.Add("serie", typeof (string));
DataColumn dc = new DataColumn("serie_denumire");
dc.Expression = string.Format("{0}+' '+{1}", "denumire", "serie");
dt.Columns.Add(dc);

The column "serie" can have null values (from the DB) but "denumire" always has a value. They're both strings and when I try to concatenate them and a row in "serie" is null the end result ("denumire" + "serie") is null.

Same with:

 dt.Columns.Add("denumire", typeof(string));  
 dt.Columns.Add("serie", typeof (string));  
 dt.Columns.Add("serie_denumire", typeof (string), "denumire + ' ' + serie");  

I should mention that the end result is displayed in a combobox:

 combobox1.DisplayMember = "serie_denumire";

PS: sorry for the formatting (4 spaces for code does not seem to work).

Upvotes: 0

Views: 1295

Answers (1)

Slippery Pete
Slippery Pete

Reputation: 3110

You can use IsNull() in your expression:

dc.Expression = string.Format("{0}+' '+IsNull({1}, '')", "denumire", "serie");

Upvotes: 1

Related Questions