突出显示重复的行,但仅在ASP.Net中的GridView中使用“名称”字段匹配重复项

时间:2016-11-21 13:34:53

标签: asp.net gridview

突出显示重复的行,但仅在ASP.Net的GridView中使用“名称”字段匹配重复项。如果任何名称超过一次重复意味着高度点亮行。

下面是匹配所有行的代码,但我想使用名称匹配重复。

<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="Name" HeaderText="Name" ItemStyle-Width="150" />
    <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" />
</Columns>
</asp:GridView>

 protected void Page_Load(object sender, EventArgs e)
 {
if (!this.IsPostBack)
{
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
                    new DataColumn("Name", typeof(string)),
                    new DataColumn("Country",typeof(string)) });
    dt.Rows.Add(1, "John Hammond", "United States");
    dt.Rows.Add(2, "Mudassar Khan", "India");
    dt.Rows.Add(3, "Suzanne Mathews", "France");
    dt.Rows.Add(4, "Robert Schidner", "Russia");
    dt.Rows.Add(4, "Robert Schidner", "Russia");
    GridView1.DataSource = dt;
    GridView1.DataBind();
    HighlightDuplicate(this.GridView1);
 }
 }

public void HighlightDuplicate(GridView grv)
{
//use the currentRow to compare against
for (int currentRow = 0; currentRow < grv.Rows.Count - 1; currentRow++)
{
    GridViewRow rowToCompare = grv.Rows[currentRow];
    //specify otherRow as currentRow + 1
    for (int otherRow = currentRow + 1; otherRow < grv.Rows.Count; otherRow++)
    {
        GridViewRow row = grv.Rows[otherRow];

        bool duplicateRow = true;
        //compare cell ENVA_APP_ID between the two rows
        if (rowToCompare.Cells[0].Text != row.Cells[0].Text)
        {
            duplicateRow = false;
            break;
        }
        //highlight both the currentRow and otherRow if ENVA_APP_ID matches
        if (duplicateRow)
        {
            rowToCompare.BackColor = Color.Red;
            rowToCompare.ForeColor = Color.Black;
            row.BackColor = Color.Red;
            row.ForeColor = Color.Black;
        }
       }
      }
     }

我在下面的例子中使用过。

http://www.aspforums.net/Threads/419884/Highlight-duplicate-rows-in-GridView-in-ASPNet/

我原来的网格代码。

 <asp:GridView Visible="true" ID="gv_candidates" runat="server" AutoGenerateColumns="false" CssClass="table table-striped table-bordered" DataKeyNames="Sno,BatchID"
                     OnRowCancelingEdit="gv_candidates_RowCancelingEdit"
                    OnRowDeleting="gv_candidates_RowDeleting" OnRowEditing="gv_candidates_RowEditing" OnRowUpdating="gv_candidates_RowUpdating" OnRowDataBound="gv_candidates_RowDataBound">
                    <Columns>
                        <asp:TemplateField HeaderText="Batch Name">
                            <ItemTemplate>
                                <asp:Label ID="lbl_BatchName" runat="server" Text='<%# Eval("BatchName") %>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txt_BatchName" runat="server" Text='<%# Eval("BatchName") %>' CssClass="form-control input-md" autocomplete="off" ></asp:TextBox>
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Batch ID">
                            <ItemTemplate>
                                <asp:Label ID="lbl_BatchID" runat="server" Text='<%# Eval("BatchID") %>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txt_BatchID" runat="server" Text='<%# Eval("BatchID") %>' CssClass="form-control input-md" autocomplete="off" ></asp:TextBox>
                            </EditItemTemplate>
                        </asp:TemplateField>

                        <asp:TemplateField HeaderText="Batch Location">
                            <ItemTemplate>
                                <asp:Label ID="lbl_BatchLocation" runat="server" Text='<%# Eval("BatchLocation") %>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txt_BatchLocation" runat="server" Text='<%# Eval("BatchLocation") %>' CssClass="form-control input-md" autocomplete="off" ></asp:TextBox>
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Candidate ID">
                            <ItemTemplate>

                                 <asp:Label ID="lbl_CandidateID" runat="server" Text='<%# Eval("CandidateID") %>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txt_CandidateID" runat="server" Text='<%# Eval("CandidateID") %>' CssClass="form-control input-md" autocomplete="off" ></asp:TextBox>
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Candidate Name">
                            <ItemTemplate>
                                <asp:Label ID="lbl_CandidateName" runat="server" Text='<%# Eval("CandidateName") %>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txt_CandidateName" runat="server" Text='<%# Eval("CandidateName") %>' CssClass="form-control input-md" autocomplete="off" ></asp:TextBox>
                            </EditItemTemplate>
                        </asp:TemplateField>

                        <asp:TemplateField HeaderText="Edit / Delete">
                            <EditItemTemplate>
                                <asp:ImageButton ID="imgbtn_Update" runat="server" ValidationGroup="bulk" CommandArgument='<%#Eval("Sno") %>'
                                    CommandName="Update" ImageUrl="~/assets/img/update1.png" ToolTip="Update" />
                                <asp:ImageButton ID="imgbtn_Cancel" runat="server" CommandName="Cancel" ImageUrl="~/assets/img/cancel.png"
                                    ToolTip="Cancel" CommandArgument='<%#Eval("Sno") %>' />
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:ImageButton ID="imgbtn_Edit" runat="server" CommandName="Edit" ImageUrl="~/assets/img/edit1.png"
                                    ToolTip="Edit" CommandArgument='<%#Eval("Sno") %>' ValidationGroup="bulk" />
                                <asp:ImageButton ID="imgbtn_Delete" runat="server" CommandName="Delete" ImageUrl="~/assets/img/delete1.png"
                                    Text="Edit" ToolTip="Delete" CommandArgument='<%#Eval("Sno") %>' />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>

1 个答案:

答案 0 :(得分:0)

在我看来,不是编写一个单独的方法来遍历网格行,而是尝试使用RowDataBound()事件并为下面的行着色。

     public List<string> DupStrings = new List<string>();
     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
     {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                 if (DupStrings.Contains(e.Row.Cells[0].Text))
                 {
                    // Color Row
                 }
                 else
                 {
                    // Add text to list
                    DupStrings.Add(e.Row.Cells[0].Text);
                 }
             }
     }