I have menustrip and each of items have its combobox. At runtime I want combos to be loaded from sql database tables. I tried dataadapter but at runtime all combos are in blank. Please help me in this topic.
I have menustrip and each of items have its combobox
413 views Asked by Hamed Jabarian At
2
There are 2 answers
0
On
I guess you instruct me something unrelated to my question. Data must be called from sql database. I attach the shape of my form. The target combobox name is cmbCement and previous menu is tsmCement. I am not sure adding Items in property will be suitable to my form goal.
public void FrmInterface_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("server = .\\SQLEXPRESS ; DataBase = Badban; Integrated Security = true");
con.Open();
SqlDataAdapter da = new SqlDataAdapter("SelectCement", con);
SqlCommand com=new SqlCommand("SelectCement",con);
com.CommandType = CommandType.StoredProcedure;
da.SelectCommand.CommandType = CommandType.StoredProcedure;
DataSet dt = new DataSet();
da.Fill(dt);
for (int i = 0; i < dt.Tables[0].Rows.Count; i++)
{
cmbCement.DroppedDown.Add(dt.Tables[0].Rows);
cmbCement.ComboBox.DataSource = dt.Tables[0];
cmbCement.ComboBox.ValueMember = "ID";
cmbCement.ComboBox.Text = "Name";
con.Close();
}
Hum...If you're using something support databinding, Maybe you can use the MVVM-like pattern.
1.Build a Model for your database tables, you need some thing to store the states.
2.ViewModel should be something like this.
3.And fill the Model's properties when you initialize your ViewModel.
4.Set your UI. Binding your checkBox's isChecked property to the ViewModel's IsChecked property.
And the XML:(Please try to figure out the relation between father & child)
OK, I'm Talking Too Much!