2014-06-06 41 views
2

在datagridview按键事件中,如果用户选择所有单元格或仅选择任何单元格数据,我想捕获该事件。如何检查用户是否选择vb.net中的datagridview中的所有单元格或任何单元格

Private Sub dg_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles dg.KeyDown 

      If e.Control AndAlso e.KeyCode = Keys.C Then 

       If all cells select then 
        ClipboardCopyMode = EnableAlwaysIncludeHeaderText 
       Else 
        ClipboardCopyMode=EnableWithoutHeaderText 
       End if 
      End If 

    End Sub 

任何帮助请!

+1

创建一个二维数组并将每个单元格值存储在其单独的位置。或者你可以像'ColumnIndex','RowIndex','Header'和'Value'那样创建一个像'myValueClass'这样的自定义类并创建一个该类对象的列表。 '列表'。 – Shell

+0

感谢您的逻辑。我现在工作了。 – Sokea

回答

2

有几个DataGridView方法和属性可以帮助你在这里:SelectedCells.Count,SelectedCells.Rows.CountSelectedCells.GetColumnCount方法。

SelectedCells.Count是不言自明的。

为了获得细胞的总数在一个DataGridView,你可以乘行数和列数,使用SelectedCells.Rows.Count属性和方法SelectedCells.GetColumnCount,在DataGridViewElementStates.Visible参数传递,以保证隐藏的列不用于计算中。

你需要做的是确定所选单元格的数量等于细胞的总数量在DataGridView中,像这样:

Private Function AllCellsSelected(dgv As DataGridView) As Boolean 
    AllCellsSelected = (DataGridView1.SelectedCells.Count = (DataGridView1.RowCount * DataGridView1.Columns.GetColumnCount(DataGridViewElementStates.Visible))) 
End Function 

我建议你创建一个布尔标志,表示如果所有细胞选中并使用AllCellsSelected方法在DataGridView的SelectionChanged事件处理程序中设置此标志。检查您的KeyDown中此标志的值。

相关问题