vb.net / DataGridView / ComboBoxCell?

喜你入骨 提交于 2019-12-25 06:19:20

问题


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

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