0

I have a Combobox, in which let's say that an item's Display Text is "School" and it's Item Value is 19. So i have stored this 19 into a DataGrid.

Then, i retrieve Combobox Value from DataGrid, then what i want to do is simply that based on value retrieved from DataGrid, combobox should set it's display Item or SelectedItem which have Value 19. In above scenario Combobox should display its selected item "School" if its value was 19.

So far i have wrote code upto this point. But it always giving me First Item of a Combobx.

            DataGrid gd = (DataGrid)sender;
        DataRowView rowSelected = gd.SelectedItem as DataRowView;
        if(rowSelected!=null)
        {
            for (int i = 0; i < comboBox1.Items.Count;i++ )
            {
                if (Convert.ToString(comboBox1.SelectedValue) == Convert.ToString(rowSelected[14]))
                {
                    index = comboBox1.Items.IndexOf(comboBox1.SelectedValue);
                }
                comboBox1.SelectedItem= comboBox1.Items[index];
            }
            textBox9.Text=rowSelected[14].ToString();

        }
Niazi
  • 105
  • 11

2 Answers2

0

Change your code to

if(rowSelected!=null)
{
    int index = comboBox1.Items.IndexOf(rowSelected[14]);
    comboBox1.SelectedItem = comboBox1.Items[index];
}

or

Use FindStringExact() method of combobox

int i = comboBox1.FindStringExact("Combo"); 
if(i >= 0)
{
}
Lucifer
  • 1,594
  • 2
  • 18
  • 32
0

Now i am able to retrieve the Combobox Item, Based on its value which i am retrieving from the WPF DataGrid.

            for (int i = 0;i <comboBox1.Items.Count; i++)
            {
                comboBox1.SelectedIndex = i;
                if ((string)(comboBox1.SelectedValue) == Convert.ToString(rowSelected[14]))
                {
                    index = i;
                }

            }

            comboBox1.SelectedIndex = index;
Niazi
  • 105
  • 11