开发者

c# combobox in datagridview

开发者 https://www.devze.com 2023-04-02 22:21 出处:网络
i\'m using datagridviewcomboboxcell to displ开发者_StackOverflow社区ay values. I wish this cells value is int. But: when I open combobox I wish to see a description of each item in combo box. When I p

i'm using datagridviewcomboboxcell to displ开发者_StackOverflow社区ay values. I wish this cells value is int. But: when I open combobox I wish to see a description of each item in combo box. When I pick one the value changes (the combobox is closed) and visible value is only int. Does anyone have idea how to do this?

Like: selected item: 2

Expanded combobox: 0 - ene 1 - due 2 - rike 3 - fake Thanks for help.


Even if you would find a way to set the TextField and ValueField I seriously doubt that you would have different text rendered while the control shows the drop down list (number and text) and while it's collapsed and shown only as drop down closed (number only).

what you ask make sense of course but you are not going to get it running with no effort just setting few properties.

To handle this in windows forms you should intercept the event: EditingControlShowing and in there, for ComboBox, specify custom values for the control, here is a starting point:

 void dataGridView1_EditingControlShowing(object sender,
            DataGridViewEditingControlShowingEventArgs e)
        {
            if (e.Control is ComboBox)
            {
                ComboBox cmb = e.Control as ComboBox;

                // here you can work on the ComboBox...
            }
        }

for more details check here: DataGridView.EditingControlShowing Event


You need to set the ValueMember property to be whatever property in your object is the "id." To set the visible text, assign the property of your object that you want visible to the DisplayMember property. If this datagrid is being bound to a database record or some sort, the properties mentioned above should be the names of the table fields you want bound.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号