【问题标题】:Trying to delete entire row in datagridview if all cells in row have same value如果行中的所有单元格具有相同的值,则尝试删除 datagridview 中的整行
【发布时间】:2013-12-23 04:58:25
【问题描述】:

我有一个数据网格视图,我在其中通过循环进行填充,然后使用单元格绘画来制作所有具有 te 值“$ 0.00”的单元格。我现在要做的是在我的填充循环执行后,我想遍历每一行,如果每个单元格包含“$ 0.00”,我想删除整行(包括行标题)。我怎么能用循环来做到这一点?我在互联网上环顾四周,发现了“Datagridview1.rows.remove(datagridiew1[row])”。可以实施以帮助我实现这一目标吗?如果是这样,怎么做?示例代码将不胜感激。谢谢!

*已编辑以包含代码*

我有两个 Subs,我在填充 datagridview sub 之后调用检查行

    Sub PopulateDataGridView()
    pb.Value = 0
    pb.Visible = True
    pb.Enabled = True
    'Loop through each column
    Dim cIndex As Integer = 0
    Dim rIndex As Integer = 0
    While cIndex < DataGridView1.ColumnCount



        'Loop through and populate each row in column
        rIndex = 0
        While rIndex < DataGridView1.RowCount

            'pb.Value = pb.Value + 1

            If cIndex = 0 Then
                'Set row header titles
                DataGridView1.Rows.Item(rIndex).HeaderCell.Value = sheet.Range("A1").Offset(rIndex + 1, cIndex).Value()

                DataGridView1.Rows(rIndex).Cells(cIndex).Value = sheet.Range("A1").Offset(rIndex + 1, cIndex + 1).Value()
            End If
            If cIndex > 0 Then
                DataGridView1.Rows(rIndex).Cells(cIndex).Value = sheet.Range("A1").Offset(rIndex + 1, cIndex + 1).Value()
            End If

            'Set column header title
            DataGridView1.Columns(cIndex).HeaderText = sheet.Range("A1").Offset(0, cIndex + 1).Value

            'Change last cell (Result) color Red or Green to represent positive gain or negative loss
            If rIndex = RowCount - 2 Then
                If DataGridView1.Rows(rIndex).Cells(cIndex).Value < 0 Then
                    DataGridView1.Item(cIndex, rIndex).Style.BackColor = Color.Red
                    DataGridView1.Item(cIndex, rIndex).Style.ForeColor = Color.White
                End If
                If DataGridView1.Rows(rIndex).Cells(cIndex).Value > 0 Then
                    DataGridView1.Item(cIndex, rIndex).Style.BackColor = Color.Green
                    DataGridView1.Item(cIndex, rIndex).Style.ForeColor = Color.White
                End If
                If DataGridView1.Rows(rIndex).Cells(cIndex).Value = 0 Then
                    DataGridView1.Rows(rIndex).Cells(cIndex).Value = "Broke Even"
                End If

            End If

            pb.Value = pb.Value + 1
            rIndex = rIndex + 1
        End While

        'Make column unsortable
        DataGridView1.Columns(cIndex).SortMode = DataGridViewColumnSortMode.NotSortable

        cIndex = cIndex + 1

    End While
    pb.Visible = False
    pb.Value = 0
    pb.Enabled = False

    DataGridView1.AutoResizeColumns()

    'Resize all Row Headers so user can see Row Titles without resizing
    DataGridView1.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders)
End Sub

Sub EmptyRowCheck()
    Dim SkipRemove As Boolean
    'loop through rows in datagrid
    For Each Row As DataGridViewRow In DataGridView1.Rows
        SkipRemove = False
        'loop through each cell in row
        For Each Cell As DataGridViewCell In Row.Cells
            'if value is not $0.00 then set boolean and exit inner loop
            If Not Cell.Value = " $0.00" Then
                SkipRemove = True
                Exit For
            End If
        Next
        'check if to remove the row or not
        If Not SkipRemove = True Then
            DataGridView1.Rows.Remove(Row)
        End If
    Next
End Sub

我的代码将包括

    PopulateDataGridView()
    EmptyRowCheck()

我现在遇到的问题是这种方法会跳过每隔一个空行,只删除一半的空行。

【问题讨论】:

  • 你能提供一些代码吗?

标签: vb.net datagridview delete-row datagridviewrow


【解决方案1】:

这样的事情可能会奏效:

    Dim SkipRemove As Boolean
    Dim Rowindex As Integer
    'loop through rows in datagrid starting from the bottom
    For Rowindex = DataGridView1.Rows.Count - 1 To 0 Step -1
        SkipRemove = False
        'loop through each cell in row
        For Each Cell As DataGridViewCell In DataGridView1.Rows(Rowindex).Cells
            If Not Cell.Value = "£0.00" Then
                SkipRemove = True
                Exit For
            End If
        Next
        'check if to remove the row or not
        If Not SkipRemove = True Then
            DataGridView1.Rows.RemoveAt(Rowindex)
        End If
    Next

这从datagridview 的底部开始,可以防止跳过问题。

【讨论】:

  • 这在大多数情况下都有效。唯一不寻常的是它跳过了每隔一个空行,所以它只删除了一半的空行。知道为什么吗?
【解决方案2】:

它应该看起来像这样:

Dim RemoveThis AS Boolean = True

FOR EACH dgvrow AS Datagridviewrow IN Datagridview1.Rows
'Loop thru each row

    FOR i AS Integer = 0 to Datagridview1.Columncount
    'Loop thru each column/field of the current row

        IF NOT dgvrow(i)="$0.00" THEN RemoveThis = False
        'If any one of the cells does not contain the value "$0.00", do not remove the row
    NEXT

    If RemoveThis THEN Datagridview1.Rows.Remove(dgvrow)
    RemoveThis = True
NEXT

【讨论】:

  • 我在尝试实现您的方法时收到以下错误。错误是针对 'dgvrow' 的,错误是“类 'System.Windows.Forms.DataGridViewRow' 不能被索引,因为它没有默认属性。”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-07
  • 2018-09-23
  • 2022-08-11
  • 2022-01-23
  • 2022-01-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多