DatagridView Highlight Event - WINFORM C#

喜你入骨 提交于 2019-12-13 04:10:01

问题


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

  1. name: Joe (highlighted)
  2. *user clicks the button whch in my case is edit
  3. *load edit form
  4. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!