Data in Combobox after Databinding

  • Thread starter Thread starter Frank Lamers
  • Start date Start date
F

Frank Lamers

Hi,

I have a problem with a Combobox after binding to a datatable. I displayed a column of a table with code similiar to what Tom Krueger mentioned here:

System.Data.DataSet ds = new System.Data.DataSet();
System.Data.SqlClient.SqlDataAdapter da = new
System.Data.SqlClient.SqlDataAdapter(
"Select * from Customers", conn);
da.Fill(ds);

System.Data.DataTable dt = ds.Tables[0];
this.comboBox1.DataSource = dt;
this.comboBox1.DisplayMember = "CustomerID";

This works fine, the column 'CustomerID' is displayed. Now the problem:

1. I can just assign values which are already listed in the column.
Example:
comboBox1.Text = "4711"
assigns the text '4711' only if it is listed in the column 'CustomerID' (and by this in the list of the combobox). Otherwise the text is unchanged.
2. I cannot check the Combobox whether it contains the string
Example:
if not comboBox1.Items.Contains("4711") then...
will always result in the string being NOT in the combobox regardless whether it is part in the datatable colum (and by this in the combobox) or not.

Any ideas how to solve this problem?

Regards

Frank Lamers
 
If you add a datarow to the underlying table with the value you want, it
will appear in the combobox. You can use the DataTable.Select method to
query for a given value

--
W.G. Ryan MVP Windows - Embedded

Have an opinion on the effectiveness of Microsoft Embedded newsgroups?
Let Microsoft know!
https://www.windowsembeddedeval.com/community/newsgroups
"Frank Lamers" <nomail> wrote in message
Hi,

I have a problem with a Combobox after binding to a datatable. I displayed
a column of a table with code similiar to what Tom Krueger mentioned here:

System.Data.DataSet ds = new System.Data.DataSet();
System.Data.SqlClient.SqlDataAdapter da = new
System.Data.SqlClient.SqlDataAdapter(
"Select * from Customers", conn);
da.Fill(ds);

System.Data.DataTable dt = ds.Tables[0];
this.comboBox1.DataSource = dt;
this.comboBox1.DisplayMember = "CustomerID";

This works fine, the column 'CustomerID' is displayed. Now the problem:

1. I can just assign values which are already listed in the column.
Example:
comboBox1.Text = "4711"
assigns the text '4711' only if it is listed in the column 'CustomerID'
(and by this in the list of the combobox). Otherwise the text is unchanged.
2. I cannot check the Combobox whether it contains the string
Example:
if not comboBox1.Items.Contains("4711") then...
will always result in the string being NOT in the combobox regardless
whether it is part in the datatable colum (and by this in the combobox) or
not.

Any ideas how to solve this problem?

Regards

Frank Lamers
 
Back
Top