问题
I am using vb.net 2010 and winforms and DataGridView.
The DataGridView has a DataGridViewComboBox column. When I show the form with the DGV it shows this and empty grid but the column that contains the ComboBox shows the first item on the dropdown list.
How can I have the ComboBox display nothing until it is clicked on and selected?
回答1:
Try setting the combobox selectedindex property to -1 when you initialize it. That might fix your problem, but when I do the same thing that you described, mine doesn't show any values in the combobox until I click on it. Here are the steps I took:
1. create a datagridview control.
2. right click on control and add column.
3. add DataGridViewComboBoxColumn
4. right click on control and edit columns.
5. Click on the button for "Items (Collection)".
6. Add some items
Now your control should behave how you are asking. It works fine when I run it. If it doesn't it may be a VS2010 bug since I'm running VS2008.
Edit:
When you add your items in code, just set the combobox value to Nothing:
Dim cboBrand As New DataGridViewComboBoxColumn
With cboBrand
.HeaderText = "Brand"
.Name = "Brand"
.Width = 300
.Items.Add("item1")
.Items.Add("item2")
.Items.Add("item3")
End With
Me.DataGridView1.Columns.Insert(0, cboBrand)
DataGridView1.Rows.Insert(0, New Object() {Nothing})
or if you want to set an initial value, do it like this:
DataGridView1.Rows.Insert(0, New Object() {"item2"})
来源:https://stackoverflow.com/questions/5247500/vb-net-datagridview-comboboxcell