【问题标题】:Counting filled rows in Datagridview计算 Datagridview 中的填充行
【发布时间】:2016-08-23 15:56:42
【问题描述】:

我正在尝试通过函数计算 datagridview 中填充的行数。
此函数包含 2 个参数,第一个是我要填充的列数,第二个是 datagridview 的行数。
我试过这段代码:

Function dgvAggregate(ByVal colNum As Integer, ByVal dgvcount As Integer)
    Dim x As Integer = 0
    For i As Integer = 0 To dgvcount - 1
        For j As Integer = 0 To colNum
            If Not (dg.Rows(i).Cells(j).Value = Nothing) Then
                x += 1

            End If
        Next j
    Next i
    Return x
End Function  

Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
    MsgBox(dgvAggregate(2, dg.Rows.Count))
End Sub  

但此代码返回具有值的单元格的数量!
如何根据用户输入的列数仅计算行数?

【问题讨论】:

    标签: vb.net datagridview


    【解决方案1】:

    您正在为每个具有值的单元格增加 x。您需要在 i 的循环中逐行进行测试和递增。

    您需要在循环开始时设置一个标志,然后检查任何具有 nothing 的列,如果找到则更新该标志。如果标志尚未更新,则增加 x

    Function dgvAggregate(ByVal colNum As Integer, ByVal dgvcount As Integer)
        Dim x As Integer = 0
        Dim emptyColumns as Integer = 0
    
        For i As Integer = 0 To dgvcount - 1
            emptyColumns = 0
            For j As Integer = 0 To colNum
                If dg.Rows(j).Cells(i).Value = Nothing Then
                    emptyColumns += 1
                End If
            Next j
            If emptyColumns = 0 Then
                x += 1
            End If
        Next i
    
        Return x
    End Function  
    
    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        MsgBox(dgvAggregate(2, dg.Rows.Count))
     End Sub  
    

    【讨论】:

    • 标志?!!,那是怎么回事?
    • 如果你喜欢这个答案并且它对你有用,请接受这个答案
    • 您应该能够接受答案,因为这是您的问题。我认为投票总数旁边应该有一个复选标记
    猜你喜欢
    • 2015-06-10
    • 2021-06-23
    • 1970-01-01
    • 2017-01-24
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 2019-11-02
    • 1970-01-01
    相关资源
    最近更新 更多