Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 如何在gridview中查找和删除重复的值_Vb.net_Gridview - Fatal编程技术网

Vb.net 如何在gridview中查找和删除重复的值

Vb.net 如何在gridview中查找和删除重复的值,vb.net,gridview,Vb.net,Gridview,我在做条形码项目。我可以将扫描的条形码添加到gridview列表中,但在删除扫描的重复条形码时发现了一些问题。因此,无论何时扫描条形码,它都会添加到列表中,即使是已经在列表中的条形码。我希望它高亮显示,以便用户知道复制了哪个条形码,甚至将其删除。gridview上的数据来自基于条形码的数据库 表格数据示例: Barcode | Items | 001 | one | --> this will be red 002 | two |

我在做条形码项目。我可以将扫描的条形码添加到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       
预期结果:

 Barcode |  Items   |
 001     |  one     |  
 002     |  two     |        
或者突出显示重复的文本

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

提前感谢。。。。不过我真的很感激

算法:删除重复数据

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

第二步:从第二排到最后,在整个网格中循环 并检查名称列的值是否相同。 如果相同,则用空字符串替换该值 否则继续使用新值。上述过程将重复


您是否使用断点检查代码在哪一行停止?如果e.Row.Cells3.Text=sender.RowsidxPrev.Cells3.Text,它会通过这一行吗?如果是,值是否正确?代码不会针对gridview上的每个值循环。。。它只检查当前索引中的前一个索引。该行工作,但循环不工作。您不需要在GridView1_RowDataBound事件中使用循环,因为RowDataBound本身的工作方式类似于通过gridview行的循环,因此您只需检查值,并在重复时移除或高亮显示它Hanks Pavan。我试过你的建议。但它显示错误索引超出范围。必须为非负数且小于集合的大小。。。。在此代码中:oldvalue=GridView1.Rows0.Cells0.TextHmmm。。。您是否有存储所有条形码值的数据库?或者您只想检查DataGridView中的所有数据?我有数据库,然后使用文本框从数据库中选择数据,并将其逐个添加到gridview中。每次输入都将添加数据。网格中是否至少有一行?好啊我会稍微修改一下代码试试。谢谢你,库马尔,,,我真的很感激。代码ealready可以工作,但它仍然能够添加重复的值。。。。
 Barcode |  Items   |
 001     |  one     |  --> this will be red
 001     |  one     |   --> this will be red     
 002     |  two     |  
 001     |  one     | 
 Barcode |  Items   |
 001     |  one     |  
 002     |  two     |        
//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