Combo Box not response

Ksilenio

Member
Joined
Jan 7, 2021
Messages
10
Programming Experience
3-5
Hi I have this code
Dim ComboBox1 As New DataGridViewComboBoxCell

ComboBox1.Items.Add("0")
ComboBox1.Items.Add("1")

DG1(2, 2) = ComboBox1

But when the form running the combo box not response . I don't have the drop down menu with "0" , "1" value .
Can you tell me anyone where is the error?
Regards Kostas
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,579
Location
Sydney, Australia
Programming Experience
10+
You pretty much need to bind the cell to a list and set the DisplayMember and ValueMember. That's how the value gets passed back and forth between the cell and the editing control. E.g.
VB.NET:
Dim items = {New With {.Text = "One", .Value = 1},
             New With {.Text = "Two", .Value = 2}}
Dim cell As New DataGridViewComboBoxCell With {.DisplayMember = "Text",
                                               .ValueMember = "Value",
                                               .DataSource = items}
In that case, the cell will display "One" or "Two" but the underlying value would be 1 or 2. That's how combo box cells work. They are usually used to display picklists from parent tables and insert the corresponding IDs into foreign key columns of child tables. If you're not using them that way, you still need to respect that they are designed that way and design your form accordingly.
 

Ksilenio

Member
Joined
Jan 7, 2021
Messages
10
Programming Experience
3-5
Hi @jmcilhinney i add you code but the combo box still not responding . I can't find where is the error . I send you a screenshot
scr8.jpg
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,561
Location
Norway
Programming Experience
10+
Both ways will work normally, but you can't edit the cell if the column is set to ReadOnly, or if bound to a table where that column is ReadOnly. The combobox will actually only appear when the cell enters edit mode, before that you only see an image of a closed combobox.
 

Ksilenio

Member
Joined
Jan 7, 2021
Messages
10
Programming Experience
3-5
Both ways will work normally, but you can't edit the cell if the column is set to ReadOnly, or if bound to a table where that column is ReadOnly. The combobox will actually only appear when the cell enters edit mode, before that you only see an image of a closed combobox.
Ok Thank you for your answer
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,579
Location
Sydney, Australia
Programming Experience
10+
If you want a combo box control in every cell in the column then why are you creating the cells directly? Why aren't you just creating a combo box column in the first place and letting it create the cells? You can then bind your list to the column and it will automatically propagate that binding to each cell, which then propagate it to the editing control when that cell is edited.
 

Ksilenio

Member
Joined
Jan 7, 2021
Messages
10
Programming Experience
3-5
If you want a combo box control in every cell in the column then why are you creating the cells directly? Why aren't you just creating a combo box column in the first place and letting it create the cells? You can then bind your list to the column and it will automatically propagate that binding to each cell, which then propagate it to the editing control when that cell is edited.
Ok Thank you for your advice
 
Top Bottom