VB.Net - 有没有办法计算 datagridview 中选定的行数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18820843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB.Net - Is there any way to count the number of selected rows in a datagridview?
提问by Jeffrey Lopez
I need to display the number of rows selected in a DataGridView to a label whenever the user selected at least one. But I have no idea on how to do it. Also I want to know what event should I use.
每当用户选择至少一个时,我需要将 DataGridView 中选择的行数显示到标签中。但我不知道该怎么做。我也想知道我应该使用什么事件。
回答by Nadeem_MK
To get the number of selected rows, you may use
要获取所选行的数量,您可以使用
Int32 selectedRowCount = dataGridView1.Rows.GetRowCount(DataGridViewElementStates.Selected)
And then to display in in a label;
然后在标签中显示;
lblLabel = selectedRowCount.ToString()
回答by karz
you need to set YourGridView.MultiSelect=true;MultiSelect When the MultiSelect property is set to true, multiple elements (cells, rows, or columns) can be selected in the DataGridView control. To select multiple elements, the user can hold down the CTRL key while clicking the elements to select. Consecutive elements can be selected by clicking the first element to select and then, while holding down the SHIFT key, clicking the last element to select.
then you can use SelectRows.
需要设置YourGridView.MultiSelect=true;MultiSelect 当MultiSelect 属性设置为true 时,可以在DataGridView 控件中选择多个元素(单元格、行或列)。要选择多个元素,用户可以在单击要选择的元素的同时按住 CTRL 键。通过单击要选择的第一个元素,然后在按住 SHIFT 键的同时单击要选择的最后一个元素,可以选择连续的元素。那么你可以使用 SelectRows。
MessageBox.Show(yourDataGridView.SelectedRows.Count.ToString());
回答by Koen
The Event you are looking for is the SelectionChangedevent of the grid.
您要查找的事件是网格的SelectionChanged事件。
You should set the MultiSelectproperty of the grid to trueto allow multiple selections.
And to get the number of selected rows you can use the SelectedRowsproperty:
您应该将网格的MultiSelect属性设置true为允许多选。要获得所选行的数量,您可以使用SelectedRows属性:
MyLabel.Text = MyGrid.SelectedRows.Count().ToString()
回答by Pradeep Ramasamy
Data Grid Mouse Down Event
Dim CRow As Int32 = DataGridView.HitTest(e.X, e.Y).RowIndex
DataGridView.Rows(CRow).Cells(ColumnName).Value()

