Multiselect DataGridView没有CTRL键,没有闪烁? [英] Multiselect DataGridView without CTRL key with no flicker?
问题描述
我的DataGridView被称为dgvOEE,我创建了一个列表,并通过CellClick事件单击它们时删除行。然后我通过PerformSelection例程选择该行。
私有selectedRows作为新列表(DataGridViewRow)
Private Sub dgvOEE_CellClick(sender As Object,e作为System.Windows.Forms.DataGridViewCellEventArgs)处理dgvOEE.CellClick
如果(selectedRows.Contains(dgvOEE.Rows(e.RowIndex)))然后
selectedRows.Remove(dgvOEE.CurrentRow)
Else
selectedRows.Add(dgvOEE.CurrentRow)
End If
PerformSelection()
End Sub
Private Sub PerformSelection()
For每个dgvRow As DataGridViewRow在dgvOEE.Rows
如果(selectedRows.Contains(dgvRow))然后
dgvRow.Selected = True
Else
dgvRow.Selected = False
结束如果
下一个
End Sub
此方法的问题是每次用户点击任何单元格,使其不亮,取消选择已经选择的任何单元,然后运行我的代码。它会导致闪烁。我相信我需要捕获/覆盖DataGridView mousedown。我看到的例子是这样的(我可以把它放到我现在的表单类中,但是我如何实现这样的东西来捕获我的表单上的DataGridView的事件?这个例子创建一个名为MyDataGrid的类,它继承DataGridView并且应该捕获OnCellMouseDown,但不知道我的表单类中的这个类如何工作(如何实现?)
公共类MyDataGrid
继承DataGridView
受保护的覆盖Sub OnCellMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
MyBase.OnCellMouseDown(e)
End Sub
结束类
有可能是一个EventHandler吗?任何帮助和代码示例将不胜感激。 p>
Kindest Regards
创建DataGridView的子类将会工作你重写OnMouseDown和OnMouseUp方法,如下所示:
公共clas s SimpleMultiselectDataGridView
继承DataGridView
受保护的覆盖Sub OnCellMouseDown(e As DataGridViewCellMouseEventArgs)
Me.Rows(e.RowIndex).Selected = Not Me.Rows(e.RowIndex)。选择
End Sub
受保护的覆盖Sub OnCellMouseUp(e As DataGridViewCellMouseEventArgs)
End Sub
结束类
然后,您可以简单地将DataGridView的类型更改为SimpleMultiselectDataGridView。
I have a DataGridView control on a form (Form1.vb) and need to allow a user to multiselect rows without using the CTRL key (no keyboard is available - they are using a touch screen). I have enabled the mutliselect property and have the following code in my Form class.
My DataGridView is called dgvOEE and I've created a List of selected rows which I add to and remove rows as they are clicked via the "CellClick" event. I then select the row via the PerformSelection routine.
Private selectedRows As New List(Of DataGridViewRow)
Private Sub dgvOEE_CellClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvOEE.CellClick
If (selectedRows.Contains(dgvOEE.Rows(e.RowIndex))) Then
selectedRows.Remove(dgvOEE.CurrentRow)
Else
selectedRows.Add(dgvOEE.CurrentRow)
End If
PerformSelection()
End Sub
Private Sub PerformSelection()
For Each dgvRow As DataGridViewRow In dgvOEE.Rows
If (selectedRows.Contains(dgvRow)) Then
dgvRow.Selected = True
Else
dgvRow.Selected = False
End If
Next
End Sub
The issue with this method is that each time a user clicks down on any cell it unhighlights/unselects anything already selected and then runs my code. It causes "flicker". I believe I need to capture/override the DataGridView mousedown. The examples I've seen are something like this (which I can put into my current form class, but how do I implement something like this to capture the event of the DataGridView on my form?? This example creates a class called MyDataGrid which inherits DataGridView and is supposed to capture the OnCellMouseDown, but not sure how this class within my form class works (how to implement?)
Public Class MyDataGrid
Inherits DataGridView
Protected Overrides Sub OnCellMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
MyBase.OnCellMouseDown(e)
End Sub
End Class
Maybe an EventHandler somehow? Any help and code examples would be appreciated.
Kindest Regards
Creating a sub class of the DataGridView will work if you override the OnMouseDown and OnMouseUp methods like so:
Public Class SimpleMultiselectDataGridView
Inherits DataGridView
Protected Overrides Sub OnCellMouseDown(e As DataGridViewCellMouseEventArgs)
Me.Rows(e.RowIndex).Selected = Not Me.Rows(e.RowIndex).Selected
End Sub
Protected Overrides Sub OnCellMouseUp(e As DataGridViewCellMouseEventArgs)
End Sub
End Class
Then you can simply change the type of your DataGridView to SimpleMultiselectDataGridView.
这篇关于Multiselect DataGridView没有CTRL键,没有闪烁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!