Multiselect DataGridView没有CTRL键,没有闪烁? [英] Multiselect DataGridView without CTRL key with no flicker?

查看:251
本文介绍了Multiselect DataGridView没有CTRL键,没有闪烁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表单(Form1.vb)上有一个DataGridView控件,并且需要允许用户在不使用CTRL键的情况下多行选择行(没有键盘可用 - 他们正在使用触摸屏)。我已经启用了mutliselect属性,并在我的Form类中具有以下代码。



我的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屋!

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