【问题标题】:how to find and remove duplicates values in gridview如何在gridview中查找和删除重复值
【发布时间】:2014-06-05 01:46:38
【问题描述】:

我正在从事条形码项目。我可以将扫描的条形码添加到 gridview 列表中,但我发现删除扫描的重复条形码存在一些问题。因此,每当扫描条码时,即使对于已经在列表中的条码,它也会添加到列表中。我希望它突出显示它,以便用户知道哪个条形码被复制甚至删除。 gridview 上的数据取自基于条形码的数据库。

示例表数据:

 Barcode |  Items   |
 001     |  one     |  --> this will be red
 002     |  two     |        
 002     |  two     |  --> this will be red
 001     |  one     | --> this will be red

这是我的代码:

    Protected Sub GridView1_RowDataBound(ByVal sender As GridView, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
  Dim oldvalue, newvalue As String
    oldvalue = String.Empty
    newvalue = String.Empty
    For j As Integer = 0 To j < 2 Step 1
        For i As Integer = 0 To i < GridView1.Rows.Count Step 1
            oldvalue = GridView1.Rows(i).Cells(j).Text
            If oldvalue = newvalue Then
                GridView1.Rows(i).Cells(j).Text = String.Empty
            End If
            newvalue = oldvalue
        Next
    Next

End Sub       

但它似乎不起作用......它没有改变任何东西......重复的仍然显示。

我什至发现这个可以将重复的颜色更改为红色,并稍微更改一下,因为它只检测以前的索引,但仍然相同......

Protected Sub GridView1_RowDataBound(ByVal sender As GridView, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
    For Each row As GridViewRow In GridView1.Rows
       If e.Row.RowType = DataControlRowType.DataRow Then
            Dim idxPrev As Integer = e.Row.RowIndex - 1
            If 1 <= e.Row.RowIndex Then
                If e.Row.Cells(3).Text = sender.Rows(idxPrev).Cells(3).Text Then
                    e.Row.ForeColor = Drawing.Color.Red
                    sender.Rows(idxPrev).ForeColor = Drawing.Color.Red
                End If
            End If
        End If
    Next

End Sub

这段代码只能显示如下内容:

 Barcode |  Items   |
 001     |  one     |  --> this will be red
 001     |  one     |   --> this will be red     
 002     |  two     |  
 001     |  one     | 

预期结果:

 Barcode |  Items   |
 001     |  one     |  
 002     |  two     |        

或突出显示重复的文本。

我不知道我的代码出了什么问题。

在此先感谢....虽然我真的很感激..

【问题讨论】:

  • 您是否使用断点检查代码在哪一行停止?它通过这条线If e.Row.Cells(3).Text = sender.Rows(idxPrev).Cells(3).Text 吗?如果是,值是否正确?
  • 代码不会为 gridview 上的每个值循环...它只检查当前索引中的前一个索引。那条线工作,但循环不工作。
  • 您不需要在 GridView1_RowDataBound 事件中使用循环,因为 RowDataBound 本身就像在您的 gridview 行中循环一样工作,因此您只需要检查该值并在它出现时删除或突出显示它重复

标签: vb.net gridview


【解决方案1】:

算法:删除重复数据

第 1 步:存储第一行、第一个单元格的内容

第 2 步:从第 2 行到最后循环遍历整个网格 并检查 Name 列的值是否相同。 如果相同,则将值替换为空字符串 否则继续使用新值。上述过程会不断重复。

//Step 1:
Dim oldvalue As String
oldvalue = String.Empty
If GridView1.Rows.Count>0 then
   string oldvalue = GridView1.Rows[0].Cells[0].Text;

   //Step 2:
   For i As Integer = 1 To i < GridView1.Rows.Count Step 1
       If oldvalue = GridView1.Rows[i].Cells[0].Text Then
          GridView1.Rows(i).Cells(0).Text = String.Empty
          GridView1.Rows(i).Cells(1).Text = String.Empty
       Else
          oldvalue=GridView1.Rows[i].Cells[0].Text
       End If
   Next
End If

【讨论】:

  • 谢谢帕万。我试过你的建议。但它显示错误“索引超出范围。必须为非负数且小于集合的大小。” ....在这段代码中:oldvalue = GridView1.Rows(0).Cells(0).Text
  • 嗯...你有一个数据库来存储所有的条形码值吗?或者您只想检查 DataGridView 中的所有数据?
  • 我有数据库,然后使用文本框从数据库中选择数据,并将其一一添加到gridview中。每次输入都会添加数据。
  • 您是否在网格中至少有一行?好的。我会稍微改一下代码试试看。
  • 谢谢库马尔,我真的很感激。代码已经可以工作了,但它仍然可以添加重复值....
猜你喜欢
  • 1970-01-01
  • 2015-03-06
  • 1970-01-01
  • 1970-01-01
  • 2010-12-04
  • 1970-01-01
  • 1970-01-01
  • 2021-01-30
  • 1970-01-01
相关资源
最近更新 更多