locked
Combobx SelectedItem binding. The DataContext for Window is DataTable. RRS feed

  • Question

  • Hi.

     The DataContext for Window is DataTable.

    I tried to set Combobx SelectedItem binding as follows:

    SetBinding(ComboBox.SelectedItemProperty, new Binding(row.Table.Columns["Value1"].ColumnName)

    But it is not working.


    Pavan Nanduri

    Wednesday, September 14, 2016 11:58 AM

Answers

  • If the ItemsSource of the ComboBox is an ArrayList of Sample objects you should set or bind the SelectedItem property to a Sample object. Or you could set or bind the SelectedValue to an int. You should not set the SelectedItem nor the SelectedValue property to a name of a DataColumn.

    If your DataTable contains a code that is present in the ComboBox, you should be able to bind the SelectedValue property to the second column of the first row of the DataTable like this:

    comboBox1.SetBinding(ComboBox.SelectedValueProperty, new Binding("[0][1]"));

    This assumes that the second column (index = 1) returns a Code (int).

    The important thing is that the type of the SelectedValue source property and the SelectedValuePath property is the same.

    But please provide all necessary code snippets - including sample data - for anyone to be able to easily reproduce your issue if you need any further help on this.

    Hope that helps.

    Please close this thread by marking all helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Proposed as answer by DotNet Wang Saturday, September 24, 2016 1:35 AM
    • Marked as answer by DotNet Wang Wednesday, September 28, 2016 2:23 AM
    Saturday, September 17, 2016 4:00 PM

All replies

  • Hi Pavan,

    Thank you for your post.

    >>"SetBinding(ComboBox.SelectedItemProperty, new Binding(row.Table.Columns["Value1"].ColumnName)"

    Is it equals to SetBinding(ComboBox.SelectedItemProperty, "Value1")?  If you want bind value from DataTable, you could use follow path.

    Suppose I want to show a cell value of first row in a TextBox, I could using following code.

    textBox.SetBinding(TextBox.TextProperty, new Binding("Rows[0][Value1]"));

    In addition, the value of ComboBox.SelectedItemProperty depends on the ItemSource and ItemTemplate of ComboBox. Please provide the ItemSource of ComboBox for further discussion.

    Best Regards,
    Li Wang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, September 15, 2016 5:10 AM
  • Li.

    Thanks for reply.

    Arraylist is being used as itemsSource for combobox.

    ArrayList is a collection of following custom object.

    Class Sample

    {

    public int Code{get; set; };

    pubic string Description{get; set;};

    }

    SelectedValuePath is Code.

    DisplayMemberPath is Description.


    Pavan Nanduri

    Friday, September 16, 2016 5:21 AM
  • If the ItemsSource of the ComboBox is an ArrayList of Sample objects you should set or bind the SelectedItem property to a Sample object. Or you could set or bind the SelectedValue to an int. You should not set the SelectedItem nor the SelectedValue property to a name of a DataColumn.

    If your DataTable contains a code that is present in the ComboBox, you should be able to bind the SelectedValue property to the second column of the first row of the DataTable like this:

    comboBox1.SetBinding(ComboBox.SelectedValueProperty, new Binding("[0][1]"));

    This assumes that the second column (index = 1) returns a Code (int).

    The important thing is that the type of the SelectedValue source property and the SelectedValuePath property is the same.

    But please provide all necessary code snippets - including sample data - for anyone to be able to easily reproduce your issue if you need any further help on this.

    Hope that helps.

    Please close this thread by marking all helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Proposed as answer by DotNet Wang Saturday, September 24, 2016 1:35 AM
    • Marked as answer by DotNet Wang Wednesday, September 28, 2016 2:23 AM
    Saturday, September 17, 2016 4:00 PM