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.
-
1Show some evidence of what you have tried. – Paul Zahra Jun 25 '15 at 07:51
2 Answers
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.
public class CheckState
{
public bool IsChecked { get; set; }
public CheckState(bool isChecked)
{
this.IsChecked = isChecked;
}
}
public class Model
{
public bool[] CheckStates { get; set; }
}
2.ViewModel should be something like this.
public class ViewModel : INotifyPropertyChanged //this is important
{
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private Model model;
public List<CheckState> checkStates = new List<CheckState>();
public List<CheckState> CheckStates
{
get { return checkStates; }
set
{
checkStates = value;
RaisePropertyChanged("CheckStates"); //And this is important
}
}
public ViewModel()
{
InitializeModel();
}
private void InitializeModel()
{
//here to retrieve data from your database
model = new Model
{
CheckStates = new[] { true, false, true, false }
};
foreach(var stateItem in model.CheckStates)
{
this.checkStates.Add(new CheckState(stateItem));
}
}
}
3.And fill the Model's properties when you initialize your ViewModel.
public void InitilizeModelData()
{
// In gernal, the data comes from database
var model = new CaculatorModel()
{
IsChecked = True,
};
IsChecked = model.IsChecked;
}
4.Set your UI. Binding your checkBox's isChecked property to the ViewModel's IsChecked property.
public partial class MainWindow : Window
{
public MainWindow()
{
DataContext = new ViewModel(); //And this is also important
InitializeComponent();
}
}
And the XML:(Please try to figure out the relation between father & child)
<Menu ItemsSource="{Binding CheckStates}">
<Menu.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsChecked}"></CheckBox>
</DataTemplate>
</Menu.ItemTemplate>
</Menu>
OK, I'm Talking Too Much!

- 43
- 1
- 7
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();
}

- 1
- 1