【发布时间】: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 行中循环一样工作,因此您只需要检查该值并在它出现时删除或突出显示它重复