如何将datagridview单元格样式从默认文本框更改为vb.net中的组合框? [英] How to change a datagridview cell style from the default textbox to combobox in vb.net?

查看:208
本文介绍了如何将datagridview单元格样式从默认文本框更改为vb.net中的组合框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个datagridview从数据集中填充。



一旦填充,如果用户点击一行,最后一列应该从文本框更改为combobox。



我正在使用vb.net 2010.


在Datagridview1 CellClick事件:

 使用DataGridView1 
如果.Rows.Count = 0然后退出Sub
i = Datagridview1.currentrow.index

Dim gridComboBox作为新的DataGridViewComboBoxCell
gridComboBox.Items.Add(A)'填充Combobox
gridComboBox.Items.Add(B)'填充Combobox
gridComboBox.Items.Add C)'填充组合框
.Item(8,i)= gridComboBox
结束

但是这会导致错误:

  DataGridView中发生以下异常:
系统。 Argument.Exception:DataGridViewComboBoxCell值无效。
要替换此默认对话框,请处理DataError事件。

如果情况不可行,我希望最后一列是填充数据时的组合框类型从数据集。

  DataGridView1.DataSource = myDataSet 

提前感谢

解决方案

好的,这是我的结论的测试。
只要单元格的值与其中一个下拉选项相同,您的代码就可以工作。但是,如果您将组合框的单元格值设置为D(cmbbox只有A,B和C),则会收到此错误消息。因此,您可以在组合框中将当前值作为选项放入,确保单元格只能将A,B或C作为值,或者只是清除将其值设置为。然后这将工作。 :)


I have a datagridview which is populated from a dataset.

Once it is populated, if the user clicks on a row, the last column should change from textbox to combobox.

I am using vb.net 2010.

On the Datagridview1 CellClick event:

    With DataGridView1
        If .Rows.Count = 0 Then Exit Sub
        i = Datagridview1.currentrow.index

        Dim gridComboBox As New DataGridViewComboBoxCell
        gridComboBox.Items.Add("A") 'Populate the Combobox
        gridComboBox.Items.Add("B") 'Populate the Combobox
        gridComboBox.Items.Add("C") 'Populate the Combobox
        .Item(8, i) = gridComboBox
    End With

But this results in an error:

The following exception occurred in DataGridView:
System.Argument.Exception: DataGridViewComboBoxCell value is not valid.
To replace this default dialog please handle the DataError event.

If the situation is not feasible, I want the last column to be of type combobox upon populate of data from the dataset.

DataGridView1.DataSource = myDataSet

Thanks in advance.

解决方案

Alright, this is my conclusion from a bit of testing. Your code works as long as the value of the cell is the same as one of the drop down alternatives. But if the cell value where you put the combobox is "D" (cmbbox only has "A", "B" and "C") you will get this error message.

So either you put in the current value as an option in your combobox, make sure that the cell only can have A,B or C as value or simply just clear the cell by setting its value to "". Then this will work. :)

这篇关于如何将datagridview单元格样式从默认文本框更改为vb.net中的组合框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆