为DataGridview的Combobox列动态生成下拉列表

一直在这个问题上走入误区

误区1:总是试图像ASP.Net下那样通过FindControl获取各行的具体控件

误区2:总是试图避免产生DataGridview的DataError事件

 

看了很多文章后,终于接受了Combobox列中各单元格中控件间类似于克隆的关系的现实.

接受了这样的现实后,如果还对Combobox列有更个性化的要求,又不愿意做太多的重写工作,性能上的牺牲就是避免不了的.

可以这样弄:

第一.在适当的地方添加对Combobox下拉列表的处理

 

 
  
Private Sub DataGridView1_CellBeginEdit( ByVal sender As Object , ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles DataGridView1.CellBeginEdit

If e.ColumnIndex = 1 Then
Dim TempDataGridViewComboBoxCell As DataGridViewComboBoxCell = CType (DataGridView1(e.ColumnIndex, e.RowIndex), DataGridViewComboBoxCell)
TempDataGridViewComboBoxCell.Dispose()
TempDataGridViewComboBoxCell.Items.Add(
Rnd )
TempDataGridViewComboBoxCell.Items.Add(
Rnd )
TempDataGridViewComboBoxCell.Items.Add(
Rnd )
TempDataGridViewComboBoxCell.Items.Add(
Rnd )

End If

End Sub

 

 

第二.就是添加一个DataError事件,处理可能会产生的错误,处理错误的办法就是什么也不做.

 

 
  

Private Sub DataGridView1_DataError( ByVal sender As Object , ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles DataGridView1.DataError
' 这里空着就可以了
End Sub

 

 

 

转载于:https://www.cnblogs.com/YuanLH/archive/2010/05/16/1736937.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值