显示在datagridview上检查/取消选中的行数 [英] Displaying number of rows checked/unchecked on datagridview

查看:106
本文介绍了显示在datagridview上检查/取消选中的行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表单上有一个数据网格,带有复选框以选择所有记录。我的datagrid的列(0)是一个复选框列,在标题上,我添加了一个复选框来选择所有记录并显示所选记录数。这是我的代码:

I have a datagrid on a form with a checkbox to select all records. Column (0) of my datagrid is a checkbox column, and on the header I put a checkbox to select all records and display the number of records selected. Here is my code:

Private Sub chkSelectRecords_CheckedChanged(sender As Object, e As EventArgs) Handles chkSelectRecords.CheckedChanged

        'Procedure runs when the checkSelectRecords is clicked.
        'The loop selects all records on the datagrid when checked
        'and clears selection when unchecked.

        Dim chkRow As Integer = 0

        If chkSelectRecords.Checked = True Then

            For Each row As DataGridViewRow In grdDeleteRecord.Rows

                row.Cells(0).Value = (row.Cells(0).Value IsNot DBNull.Value)
                chkRow += 1

                lblRowCount.Visible = True
                lblRowCount.Text = chkRow.ToString & " Record(s) Selected"
           Next
        Else
            For Each row As DataGridViewRow In grdDeleteRecord.Rows

                row.Cells(0).Value = (row.Cells(0).Value Is DBNull.Value)
                chkRow += 0

                lblRowCount.Text = chkRow.ToString & " Record(s) Selected"
            Next
        End If

    End Sub

当用户一次选择一个记录时,我另外提供了一个过程:

I placed another procedure when the user selects one record at a time:

Private Sub grdDeleteRecord_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles grdDeleteRecord.CellContentClick

' Shows how to get count of checked or unchecked checkbox column count along with
' how to get column data back for checked items.

Dim chkRowCount As Integer = 0

grdDeleteRecord.EndEdit()

For Each row As DataGridViewRow In grdDeleteRecord.Rows

    If row.Cells(0).Value = True Then
        chkRowCount += 1
    Else
        chkRowCount += 0

        lblRowCount.Visible = True
        lblRowCount.Text = chkRowCount.ToString & " Record(s) Selected"

    End If
Next

End Sub

两个程序都可以根据需要工作。问题是当我选择所有记录时,我的标签不会更新现在选择的记录数。我确定这是发生的,因为它们是两个单独的事件,我只是不知道如何链接事件或编写一个完成所有三个任务的过程。

Both procedures work as needed. The problem is when I select all records my label does not update the number of records that are now selected. I am sure this is happening because they are two separate events, I just don't know how to link the events or write a procedure that accomplishes all three tasks

推荐答案

这是我为你写的一个功能。您必须使用CellValueChangedEvent和CurrentCellDirtyStateChanged事件...

Here's a function I wrote for you. You must use the CellValueChangedEvent and the CurrentCellDirtyStateChanged events...

 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    lblRowCount.Text = SetLabel()
 End Sub


Private Function SetLabel() As String
    Dim strRows As String = String.Empty
    Dim intRows As Integer = 0

    For i As Integer = 0 To grdDeleteRecord.Rows.Count - 1
       If CBool(grdDeleteRecord(0, i).Value) Then
          intRows += 1
       End If
    Next

    If intRows > 0 Then
        strRows = intRows.ToString & " Record(s) Selected"
        lblRowCount.Visible = True
    End If

    Return strRows
End Function

Private Sub grdDeleteRecord_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles grdDeleteRecord.CellValueChanged
    lblRowCount.Text = SetLabel()
End Sub

Private Sub grdDeleteRecord_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles grdDeleteRecord.CurrentCellDirtyStateChanged
    If grdDeleteRecord.IsCurrentCellDirty Then
        grdDeleteRecord.CommitEdit(DataGridViewDataErrorContexts.Commit)
    End If
End Sub

当您选中或取消选中这些复选框时,它将触发此事件,从而更新您的标签。然后你可以使用这个,你需要它来使用。

When you check or uncheck these checkboxes it will trigger this event and thus update your label. Then you can use this where ever you need it to be used.

这篇关于显示在datagridview上检查/取消选中的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆