vb.net 如何计算datagridview vb.net中选定的行数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34326155/
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
How to count the number of selected rows in datagridview vb.net
提问by CreoGuy
I'm a complete rookie. I learned so much from here but this one I can't find the answer to. I'm using Visual Studio Pro 2015.
我是一个完全的菜鸟。我从这里学到了很多东西,但我找不到答案。我正在使用 Visual Studio Pro 2015。
I found this question, but the answer does not work for my application:
我发现了这个问题,但答案对我的应用程序不起作用:
VB.Net - Is there any way to count the number of selected rows in a datagridview?
VB.Net - 有没有办法计算 datagridview 中选定的行数?
I have a windows form application that has a single column datagridview that is populated by reading a textfile, line by line at runtime.
我有一个 windows 窗体应用程序,它有一个单列 datagridview,它是通过在运行时逐行读取文本文件来填充的。
I need to display (in a label) the number of rows that the user selects in the datagridview control.
我需要显示(在标签中)用户在 datagridview 控件中选择的行数。
I have this:
我有这个:
Private Sub DataGridView1_SelectionChanged(sender As Object, e As EventArgs) Handles DataGridView1.SelectionChanged
Dim selectedRowCount As Integer
selectedRowCount = DataGridView1.Rows.GetRowCount(DataGridViewElementStates.Selected)
lblNumSelected.Text = selectedRowCount.ToString()
End Sub
Or this:
或这个:
Private Sub DataGridView1_SelectionChanged(sender As Object, e As EventArgs) Handles DataGridView1.SelectionChanged
lblNumSelected.Text = DataGridView1.SelectedRows.Count().ToString()
End Sub
Both of these methods return 0 (zero) no matter how many rows are selected. Thank you for helping me.
无论选择了多少行,这两种方法都返回 0(零)。感谢你们对我的帮助。
采纳答案by SSS
On your DataGridView, set the .SelectionModeproperty to FullRowSelect, then use the .SelectedRows.Countproperty.
在您的 上DataGridView,将.SelectionMode属性设置为FullRowSelect,然后使用该.SelectedRows.Count属性。
回答by Werkzeug
I am having no problem using
我使用没有问题
Private Sub DataGridView1_SelectionChanged(sender As Object, e As EventArgs) Handles DataGridView1.SelectionChanged
Label1.Text = DataGridView1.Rows.GetRowCount(DataGridViewElementStates.Selected)
End Sub
Could it be you're only selecting the cells themselves and not the row selector on the left side? When I select multiple cells I get 0 but using the button that selects an entire row counts correctly.
可能是您只选择单元格本身而不是左侧的行选择器?当我选择多个单元格时,我得到 0 但使用正确选择整行的按钮计数。

