ktos1234 ktos1234 - 1 month ago 6
C# Question

Two field values in Combobox displaymember and datatable C#

I want to bind two fields together and display it in combobox.

I have first name and last name in my database, how to display full name in combobox?

I tried this code but I got some errors:

OracleDataAdapter names= new OracleDataAdapter("SELECT first, last FROM person", conn);
DataTable dt = new DataTable();
names.Fill(dt);
dt.Columns.Add("FullName", typeof(string), "first' : ' last"); <-- error's here
cmbCBox.DisplayMember = "FullName";
cmbBox.DataSource = dt;
conn.Close();


Error is :


Missing operand after ':' operator.

Answer

If you want to create a format like this:

Donald : Duck

Then the expression is:

dt.Columns.Add("full", typeof(string), "first + ' : ' + last");

Example code:

System.Data.DataTable dt = new System.Data.DataTable();

dt.Columns.Add("first", typeof(string));
dt.Columns.Add("last", typeof(string));
dt.Columns.Add("full", typeof(string), "first + ' : ' + last");

System.Data.DataRow row = dt.NewRow();
row["first"] = "Donald";
row["last"] = "Duck";
dt.Rows.Add(row);