0
votes

I am binding a data table to combobox like as i show in code below .

        objComboBox.ItemsSource = objDataTableNew.DefaultView;
        objComboBox.DisplayMemberPath = objDataTableNew.Columns[0].ToString();
        objComboBox.SelectedValuePath = objDataTableNew.Columns[1].ToString();
        objComboBox.SelectedIndex = 0;

Now i want to add combo box item with display text as "select" and value as "-1" to the top of the list . Directly i cant add since itemsource is bound with data table. I tried inserting a row to objDataTableNew at index zero . but i am having a prob . 0th column of datatable obtained from DB is a integer column. So i cant insert a string value "select" to that column .

How do i achieve this ?

6
I guess [this discussion][1] shoudl give you all the info you need. [1]: stackoverflow.com/questions/199642/… - Marthin
good one @Marthin , casted yhe entries and inserted at position 0 .many thanks - Kuntady Nithesh

6 Answers

0
votes

Bind objComboBox.ItemsSource with ObservableCollection, where collection's content will be "select" string + data table items.

0
votes

Try CompositeCollection in WPF.

  <ListBox xmlns:System="clr-namespace:System;assembly=mscorlib"
    xmlns:Collections="clr-namespace:System.Collections;assembly=mscorlib">
        <ListBox.Resources>
            <Collections:ArrayList x:Key="TestCollection">
                <System:String>1</System:String>
                <System:String>2</System:String>
                <System:String>3</System:String>
                <System:String>4</System:String>
            </Collections:ArrayList>
        </ListBox.Resources>
        <ListBox.ItemsSource>
            <CompositeCollection>
                <System:String>--Select--</System:String>
                <CollectionContainer
                    Collection="{StaticResource TestCollection}"/>
            </CompositeCollection>
        </ListBox.ItemsSource>
    </ListBox>

So in place of StaticResource you can supply Binding from your View Model as well.

Hope this helps.

0
votes

The error message is pretty self explanatory. It appears you are tying to insert a string ("select") in an integer column [0]. Insert the integer "-1" in [0] (integer column) and the string "select" in [1] (the string column). Assuming [1] is a string column.

0
votes

This worked for me:

DataRow row = dt.NewRow(); 
row["Category"] = "<-Please select Category->"; 
dt.Rows.InsertAt(row, 0); 
CBParent.DataSource = dt;
-1
votes

First of all bind data to datatable and create new row to datatable

DataRow row = dt.NewRow(); row["Category"] = "<-Please select Category->"; dt.Rows.InsertAt(row, 0); CBParent.DataSource = dt; 
-1
votes
            List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>(objDataTable.Rows.Cast<DataRow>().Select(row => new KeyValuePair<string, string>(row[DisplayMemberColumn].ToString(), row[SelectedValueColumn].ToString())));
            list.Insert(0, new KeyValuePair<string, string>("<--Select-->", "-1"));
            objComboBox.ItemsSource = list;
            objComboBox.DisplayMemberPath = "Key";
            objComboBox.SelectedValuePath = "Value";
            objComboBox.SelectedIndex = 0;