When I try to add item to combobox in WPF which is already bound I get an error. How to achieve this ? My target is to add default value to the combobox.
KINDLY HELP.
public static void BindControl(ref ComboBox obj, DataTable DT, String key, String value, String defaultvalue)
{
try
{
if (DT.Rows.Count > 0)
{
obj.DataContext = DT;
obj.SelectedValuePath = DT.Columns[value].ColumnName;
obj.DisplayMemberPath = DT.Columns[key].ColumnName;
obj.SelectedIndex = 0;
ComboBoxItem x = new ComboBoxItem();
x.Content = defaultvalue;
obj.Items.Add(x);
obj.Items.Insert(0, x); //THIS THROWS ERROR
}
catch (System.Exception ex)
{
}
You have to add
Itemsinto the DataTable.