Sunday, January 31, 2010

Databinding with combobox in C# windows application

It is quite similar to the databinding with dropdown list in ASP.NET. Anyway, here is the code for databinding.
# code for databinding with combobox

try {
SqlConnection conn = new SqlConnection();
conn.ConnectionString = Properties.Settings.Default["MyConnStr"].ToString();
conn.Open();

string select_cats="select * from BusinessCategory";
SqlDataAdapter sda=new SqlDataAdapter(select_cats,conn);
DataTable dt=new DataTable();
sda.Fill(dt);

//databinding to the combobox
TestcomboBox.DataSource = dt.DefaultView;
TestcomboBox.DisplayMember = "Description";
TestcomboBox.ValueMember = "BusCatID";





}
catch (Exception exc) {

//MessageBox.Show(exc.Message);
}

Here we see 'Description' is used here as text property and 'BusCatID' is used here as value property with combo box.


No comments:

Post a Comment