winforms ComboBox DataSource
Adding connectionString in App.config file
</configSections>
<appSettings>
<add key=”ConnectionString” value=”Persist Security Info=False;Data Source=lokesh;Initial Catalog=winForms;User ID=sa;Password=xyz;” />
</appSettings>
Winform ComboBox Databinding
private void ComboBox1_Load(object sender, EventArgs e)
{
SqlConnection objConn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
SqlCommand objCmd = new SqlCommand(”select CategoryID,Category from tblCategory“, objConn);
DataSet objDataset= new DataSet();
SqlDataAdapter objDa = new SqlDataAdapter(objCmd);
objConn.Open();
objDa.Fill(objDataset);
comboBox1.DataSource = objDataset.Tables[0];
comboBox1.DisplayMember = “Category”;
//comboBox1.ValueMember = “CategoryID”;
comboBox1.ValueMember = “Category”; //Remember this
objConn.Close();
}
Winform ComboBox SelectionChanged Event
private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
{
//Getting ComboBox Selected Value
string a = comboBox1.SelectedValue.ToString();
textBox1.Text = a;
}
