Click here to Skip to main content
15,916,280 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have Form1 and Form2. In Form1 there is a dataGridView with a field called category.

In form2 I have set a combobox. I send data from field category of dataGridView in form1 to this combobox in Form2.

I have made the identifier of this combobox public. There is an update button in Form1.

I want when I select a row and click on update button, Form2 opens and the combobox that is there shows the value of field category of dataGridView of Form1.

This is the code:
SQL
Form2 fr2 = new Form2();
fr2.cmbCategory.Text = dgvProduct.SelectedRows[0].Cells[1].Value.ToString();
fr2.Show();


Then in Form2 I have set the DataSet of cmbCategory to tblCategory, and set it’s Display member to field code.

I want cmbCategory to show all items of field code in tblCategory and at the same time, one of those items be selected. And so the selected item should be the one that I pass to it from Form1.

I wonder how can it be done?
Posted
Updated 8-Jul-15 19:26pm
v2

1 solution

First of all you bind your combobox setting it DataSource property and then set its SelectedIndex or SeletectedValue property which will be selected by default.
 
Share this answer
 
Comments
Member 11796027 9-Jul-15 2:37am    
I did bind it and then set its SelectedValue property and it selects the first Item of the list by default. But I want it to select the item that I specify in Form1 by selecting the row of dataGridView. How can I do that?
Kailash Polai 9-Jul-15 7:04am    
what value is returning if you debug here?

dgvProduct.SelectedRows[0].Cells[1].Value.ToString()

it should match the item text in the dropdown items
i think i may retun null or empty
Member 11796027 9-Jul-15 7:32am    
It returns the value that is in the datagridview in the selected row and second field of that, and it is correct.

The problem is that if I do not bind ComboBox to a table the ComboBox in Form2 shows the exact value that is selected in Form1.

But when I bind it to a table's data, it does not select the item that is chosen in form1 among items that are bind to it and it just select the first item.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900