如何在gridview上查找重复值

时间:2014-06-04 11:57:37

标签: asp.net vb.net gridview

我有一个gridview数据,想要突出显示重复值。我想更改所有重复值的颜色变为红色。

gridview数据:

 ID |  Items     |
 1  |  Item1     |  --> this will be red
 2  |  Item2     |        
 1  |  Item1     |  --> this will be red
 1  |  Item1     | --> this will be red


 Protected Sub GridView1_RowDataBound(ByVal sender As GridView, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
    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
End Sub

我的代码只能更改某个数据,因为我只为前一个设置了idxPrev,所以它只能更改能够检测到以前的索引。我希望它检查并更改所有gridview重复数据。有可能这样做吗?你介意帮我解决这个问题吗?

谢谢你的进步......

2 个答案:

答案 0 :(得分:0)


请试试这个。

来源

<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="30" />
        <asp:BoundField DataField="Items" HeaderText="Items" ItemStyle-Width="150" />/>
    </Columns>
</asp:GridView>


代码背后

protected void Page_Load(object sender, EventArgs e)
{
    bind();
}
protected void bind()
{
    con.Open();
    SqlCommand cmd = con.CreateCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "select * from tblSample";
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
    GridView1.DataSource = ds;
    GridView1.DataBind();
    HighlightDuplicate(this.GridView1);
    con.Close();
}
public void HighlightDuplicate(GridView gridview)
{
    for (int currentRow = 0; currentRow < gridview.Rows.Count - 1; currentRow++)
    {
        GridViewRow rowToCompare = gridview.Rows[currentRow];
        for (int otherRow = currentRow + 1; otherRow < gridview.Rows.Count; otherRow++)
        {
            GridViewRow row = gridview.Rows[otherRow];
            bool duplicateRow = true;
            if (int.Parse(rowToCompare.Cells[0].Text) != int.Parse(row.Cells[0].Text))
            {
                duplicateRow = false;
            }
            else if (duplicateRow)
            {
                rowToCompare.BackColor = Color.Red;
                row.BackColor = Color.Red;
            }
        }
    }
}


在代码隐藏页面中添加以下命名空间。

using System.Data;
using System.Drawing;

答案 1 :(得分:0)

   /*highlight duplicate row in gridview */
 public void HighlightDuplicate(GridView GridView1)
        {
            for (int ThisRow = 0; ThisRow < GridView1.Rows.Count - 1; ThisRow++)
            {
                Label lbl_cd = (Label)GridView1.Rows[ThisRow].FindControl("lblcd");
                lblhidcd.Value = lbl_EmpCode.Text;
                GridViewRow CompareRow =`enter code here` GridView1.Rows[ThisRow];
                for (int NextRow = ThisRow + 1; NextRow < GridView1.Rows.Count; NextRow++)
                {
                    GridViewRow row = GridView1.Rows[NextRow];
                    bool DuplicateRow = true;
                    Label lbl_cd1= (Label)GridView1.Rows[NextRow].FindControl("lblcd");
                    hidnxtrowhidcd.Value = lbl_cd1= .Text;
                    if ((lblhidcd.Value) == (hidnxtrowhidcd.Value ))
                    {
                        row.BackColor = System.Drawing.Color.BlanchedAlmond;
                        CompareRow.BackColor = System.Drawing.Color.BlanchedAlmond;
                    }
                    else if (DuplicateRow)
                    {
                        DuplicateRow = false;
                    }
                }
            }
        }