.net - 如何计算datagridview vb.net中选中的行数

标签 .net vb.net winforms datagridview

我是个菜鸟。我从这里学到了很多东西,但是这个我找不到答案。我正在使用 Visual Studio Pro 2015。

我发现了这个问题,但答案不适用于我的应用程序:

VB.Net - Is there any way to count the number of selected rows in a datagridview?

我有一个 Windows 窗体应用程序,它有一个单列数据 GridView ,它是通过在运行时逐行读取文本文件来填充的。

我需要显示(在标签中)用户在 datagridview 控件中选择的行数。

我有这个:

    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

或者这个:

    Private Sub DataGridView1_SelectionChanged(sender As Object, e As EventArgs) Handles DataGridView1.SelectionChanged
        lblNumSelected.Text = DataGridView1.SelectedRows.Count().ToString()
    End Sub

无论选择了多少行,这两种方法都返回 0(零)。 谢谢你帮助我。

最佳答案

在您的 DataGridView 上,将 .SelectionMode 属性设置为 FullRowSelect,然后使用 .SelectedRows.Count属性(property)。

关于.net - 如何计算datagridview vb.net中选中的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34326155/

相关文章:

c# - 仅返回字符串中的数字 0-9

c# - ListView 中的项目没有行缩进

c# - 处理 'A potentially dangerous Request.Form value was detected from the client' 并通知用户此错误

.net - 自动夹具自动数据错误 - 未提供参数

c# - 如何反序列化接口(interface)类型?

c# - 观察具有传递项目依赖项的构建?

vb.net - SQLite,是否打开一个永久连接?

vb.net - 从 XSD 创建 dataset.designer.vb

c# - Windows 窗体屏幕保护程序预览窗口句柄

c# - WinForms 数据绑定(bind) DataGridView 与 ComboBoxColum