简体   繁体   English

vb.net/DataGridView/ComboBoxCell?

[英]vb.net / DataGridView / ComboBoxCell?

I am using vb.net 2010 and winforms and DataGridView. 我正在使用vb.net 2010和winforms和DataGridView。

The DataGridView has a DataGridViewComboBox column. DataGridView具有一个DataGridViewComboBox列。 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. 当我显示带有DGV的表单时,它显示了这个空白的网格,但是包含ComboBox的列显示了下拉列表中的第一项。

How can I have the ComboBox display nothing until it is clicked on and selected? 在单击并选择之前,如何使ComboBox不显示任何内容?

Try setting the combobox selectedindex property to -1 when you initialize it. 初始化时,尝试将组合框selectedindex属性设置为-1。 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. 如果不是,那可能是由于我正在运行VS2008而导致的VS2010错误。

Edit: 编辑:

When you add your items in code, just set the combobox value to Nothing: 在代码中添加项目时,只需将组合框值设置为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"})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM