我正在寻找这个答案。我最终编写了一个可以从任何DataGridView中调用的泛型子类,因为我的应用程序中有很多东西,我希望它们都以相同的方式运行。这对我来说非常合适,所以我想与任何偶然发现这篇文章的人分享。
在鼠标点击事件的DGV我添加代码
Private Sub SomeGrid_MouseClick(sender As Object, e As MouseEventArgs) Handles SomeGrid.MouseClick
DGV_MouseClick(sender, e)
End Sub
它调用下面子,我存储共享模块
Public Sub DGV_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Try
Dim dgv As DataGridView = sender
Dim h As DataGridView.HitTestInfo = dgv.HitTest(e.X, e.Y)
If h.RowIndex > -1 AndAlso h.ColumnIndex > -1 AndAlso dgv.Columns(h.ColumnIndex).CellType Is GetType(DataGridViewComboBoxCell) Then
Dim cell As DataGridViewComboBoxCell = dgv.Rows(h.RowIndex).Cells(h.ColumnIndex)
If Not dgv.CurrentCell Is cell Then dgv.CurrentCell = cell
If Not dgv.IsCurrentCellInEditMode Then
dgv.BeginEdit(True)
CType(dgv.EditingControl, ComboBox).DroppedDown = True
End If
End If
Catch ex As Exception
End Try
End Sub
我没有抓到任何错误的,我只包括Try..Catch代码为一些罕见的例子,我想不出可能会抛出异常。我不希望用户因为此场景的错误消息而烦恼。如果分组失败,那么DGV很可能就像通常那样运行。
我很高兴你得到它的工作! – thismat 2008-10-28 12:41:38