【问题标题】:How to count the number of selected rows in datagridview vb.net如何计算datagridview vb.net中选定行的数量
【发布时间】:2015-12-17 03:31:49
【问题描述】:

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

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

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

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

我需要显示(在标签中)用户在 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(零)。 谢谢你帮助我。

【问题讨论】:

    标签: .net vb.net winforms datagridview


    【解决方案1】:

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

    【讨论】:

    • 非常感谢。我吸取了教训,检查控件的每个属性以寻找线索。
    【解决方案2】:

    我使用没问题

    Private Sub DataGridView1_SelectionChanged(sender As Object, e As EventArgs) Handles DataGridView1.SelectionChanged
        Label1.Text = DataGridView1.Rows.GetRowCount(DataGridViewElementStates.Selected)
    End Sub
    

    您是否只选择单元格本身而不是左侧的行选择器?当我选择多个单元格时,我得到 0,但使用选择整行的按钮计数正确。

    【讨论】:

    • 你是对的,这就是问题所在。我隐藏了行选择器,因为我只有一列并且用户无法编辑列表。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-25
    • 2016-03-17
    • 2013-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多