如何在GridView中查找和删除重复值 [英] how to find and remove duplicates values in gridview

查看:218
本文介绍了如何在GridView中查找和删除重复值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从事条形码项目.我可以将扫描的条形码添加到gridview列表中,但是在删除重复扫描的条形码时发现了一些问题.因此,无论何时扫描条形码,即使列表中已有条形码,它也会添加到列表中.我希望它高亮显示它,以便用户知道哪个条形码被重复甚至删除了. gridview上的数据来自基于条形码的数据库.

I am working on barcode project. I can add the scanned barcode into gridview list, but I found some problem with removing the duplicates barcode scanned. so whenever the barcode scanned it will add to the list even for the barcode that already in the list. I want it to highlight it so that the user know which barcode was duplicated or even remove it. the data on gridview was take from a database based on barcode.

示例表格数据:

 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       

但是它似乎不起作用...它什么也没有改变...重复项仍然显示.

but it doesn't seem to work... it doesn't change anything... the duplicate still display.

我什至发现,可以将重复项的颜色更改为RED,并对其进行一些更改,因为它仅检测先前的索引,但仍然相同....

I even found this that can be change the color of duplicate into RED, and change it a bit because it's only detect the previous index, but still the same....

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

此代码只能显示以下内容:

this code only able to display something like this:

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

预期结果:

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

或突出显示重复的文本.

Or highlight the duplicate text.

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

I don't know what was wrong on my code.

预先感谢....我真的很感激..

Thanks in advances.... I really appreciate the though..

推荐答案

算法:删除重复的数据

Algorithm: Remove duplicate data

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

Step 1: Store the first row's , first cells content

第2步:从第2行到最后循环遍历整个网格 并检查名称"列的值是否相同. 如果相同,则用空字符串替换该值 否则继续使用新值.上面的过程会重复.

Step 2: Loop through the entire grid from the 2nd row till the end and check if the Name column's value is same or not. If it is same, then replace the value with empty string else continue with the new value. The above process will repeat itself.

//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

这篇关于如何在GridView中查找和删除重复值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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