santBart
santBart

Reputation: 2496

c# combobox in datagridview

i'm using datagridviewcomboboxcell to display 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.

Upvotes: 1

Views: 1017

Answers (2)

Davide Piras
Davide Piras

Reputation: 44605

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

Upvotes: 1

Tieson T.
Tieson T.

Reputation: 21239

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.

Upvotes: 0

Related Questions