突出显示gridview asp.net中的重复项

时间:2015-10-14 18:20:11

标签: c# asp.net sql-server gridview

我尝试从sql数据中检测重复项并在gridview中突出显示它们,但颜色不会改变。 我创建了BindGrid()方法并在Page_Load方法中调用了该方法。 BindGrid()运行良好,但HighlightRow根本不工作。 有什么帮助吗?

ASP:

 <asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1" GridLines="None" Height="238px" Width="896px"  onrowdatabound="HighlightRow">
    <FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
    <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
    <PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
    <RowStyle BackColor="#DEDFDE" ForeColor="Black" />
    <SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#F1F1F1" />
    <SortedAscendingHeaderStyle BackColor="#594B9C" />
    <SortedDescendingCellStyle BackColor="#CAC9C9" />
    <SortedDescendingHeaderStyle BackColor="#33276A" />
</asp:GridView>

C#代码:

protected void HighlightRow(object sender, GridViewRowEventArgs e)
    {
        if(e.Row.RowType == DataControlRowType.DataRow)
        {   
            for (int current = 0; current < GridView1.Rows.Count - 1; current++)
            {
                GridViewRow compareRow = GridView1.Rows[current];
                for (int next = current+1; next < GridView1.Rows.Count; next++)
                {
                    GridViewRow row = GridView1.Rows[next];
                    bool duplicateRow = true;
                    for (int i = 0; i < GridView1.Columns.Count; i++) {                         
                        if ((compareRow.Cells[i].Text) != (row.Cells[i].Text))
                        {
                            duplicateRow = false;
                            break;
                        }
                        if (!duplicateRow)
                        {
                            e.Row.BackColor = System.Drawing.Color.Red;
                            row.BackColor = System.Drawing.Color.Red;
                            compareRow.BackColor = System.Drawing.Color.Red;
                            duplicateRow = true;
                        }
                    }

                }
            }
        }
    }

0 个答案:

没有答案