: DataGridView中以DataGridViewComboBoxCell方式实现下拉列表方式报错

求助: DataGridView中以DataGridViewComboBoxCell方式实现下拉列表方式报错
本帖最后由 XIHONGSHI238 于 2014-12-16 13:55:40 编辑
我希望在DataGridView中单击【性别】列某单元格,下拉出备选项:"","男","女",用此方式完成表格的编辑。代码如下:
Private Sub DataGridView1_CellBeginEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles DataGridView1.CellBeginEdit
        Dim a As Integer = DataGridView1.CurrentCell.RowIndex, b As Integer = DataGridView1.CurrentCell.ColumnIndex
        Dim Cell As New DataGridViewComboBoxCell
        Select Case DataGridView1.Columns(e.ColumnIndex).HeaderText
            Case "性别"
                Cell.Items.AddRange(New Object() {"", "男", "女"})  '也可以在此处读表,动态循环添加字段值
                Cell.MaxDropDownItems = 40
                Cell.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing '确保离开当前单元格后恢复为文本框显示状态
                DataGridView1.Rows(a).Cells(b) = Cell
        End Select
End Sub

运行时效果符合预期,但是偶尔会报错退出,报错信息如下:
未处理 InvalidOperationException
操作无效,原因是它导致对 SetCurrentCellAddressCore 函数的可重入调用。


排查了很久,起初误以为是大量的历史数据中或许存在null值造成的,
后来发现:如果【性别】列是第3列的话,那么只有在第3行的位置点击单元格才会出错,也就是 a = b 时报错。我的VS版本2010
求大侠、大神们帮我分析一下到底咋回事吧,拜托拜托,谢谢谢谢!
------解决思路----------------------
单看代码无问题,代码单独测试也没问题,应该是你其他地方写的东西联动产生了问题
参考下面链接http://www..net/c-sharp/626065.html
------解决思路----------------------
引用:
我试过把那一列做成DataGridViewComboBoxColumn,可以直接在items中手动输入备选项,但是如果主表是仓单表(仓单号,物品代码,客户,物品名称,数量,单价),辅助表是客户表(客户简称,客户全程,地址,联络方式,联络人),我在主表的客户列设置为DataGridViewComboBoxColumn,希望连接到客户表的客户简称字段作为编辑时的备选项,屡试屡败啊!大神们能给段代码吗?谢谢!

把绑定附表的datatable copy一份
DataTable dt1=dt.Copy();
然后再绑定,不要绑定同一个数据源
而且你要把DataGridViewComboBoxColumn列绑定dt1,不要循环每行去绑定
------解决思路----------------------
VS的DATAGRIDVIEW的确有BUG。。。