问题
I have a combobox which is connected to the database so I populate the value of my combobox based on what's in my database. my combobox is another FORM from the datagrid. So here's I want to achieve.
form1 = datagrid (based on the database) form2 = combobox (based on the database)
I want that If I highlight a certain row (My selection mode = fullrowselect) and press a button the comboBox will automatically point to that row.
for ex. datagrid
- name: Joe (highlighted)
- *user clicks the button whch in my case is edit
- *load edit form
- comboBox.SelectedIndex is = highlighted row (which the user clicks)
I can show you my code if it helps. thanks :))
THANKS! :))
回答1:
You can try to set in the following ways, you can pass the value Joe to the other form via a parameter in the constructor. This could be then used to select you required value in the ComboBox
comboBox2.SelectedIndex = comboBox2.Items.IndexOf("Joe");
comboBox2.SelectedText = "Three";
// or SelectedValue depending on how you are binding
EDIT Avoid accessing the grid directly from the other form, expose the value required as a property or better pass it to the new form as parameter.
Joe could be the value of the cell like dataGridView2.CurrentRow[0].FormattedValue
and pass this to the new form constructor like new Form2(object datagridvalue)
. Then use the value in the form later on.
来源:https://stackoverflow.com/questions/8426053/datagridview-highlight-event-winform-c-sharp