删除GridView中的重复行

时间:2013-09-27 08:13:21

标签: c# asp.net gridview

我有三个GridView由于某种原因,前三列的第一个重复数据>这不是一个错误,因为这就是我想要它的方式。

我希望网格在上面两列Client NameBranch中的副本中留空。我循环遍历我的单元格并将其设置为Databound上的空白,如下所示。

protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
{
    string oldValue = string.Empty;
    string newValue = string.Empty;
    //  for (int count = 0; count < gvList.Rows.Count; count++)
    for (int j = 0; j < 2; j++)
    {
        for (int count = 0; count < gvList.Rows.Count; count++)
        {
            oldValue = gvList.Rows[count].Cells[j].Text;
            if (oldValue == newValue)
            {
                gvList.Rows[count].Cells[j].Text = string.Empty;
            }
            newValue = oldValue;
        }
    }
}

跑完后我得到下面的结果。网格执行我想要的行,并省略行进行。我很迷惑。请更好的替代品将不胜感激。

我的第二个问题是,我有另一个网格也重复了几行。我应用了相同的方法,Dispose()复制但没有任何变化。我用

gridview.rows[].cells.clear/disposed()` 

我的视角如下。

 <asp:GridView ID="gvList" runat="server" AutoGenerateColumns="False" AllowPaging="true" AllowSorting="true" OnSorting="gvList_sorting" 
                   DataKeyNames="contactID" OnPageIndexChanging="gvList_PageIndexChanging" OnRowCancelingEdit="gvList_RowCancelingEdit" 
                   OnRowDeleting="gvList_RowDeleting"  OnRowEditing="gvList_RowEditing" OnRowUpdating="gvList_RowUpdating" OnRowDataBound="gvList_RowDataBound"
               EnableModelValidation="True" CellPadding="4" ForeColor="#333333" GridLines="None" ShowFooter="True">


                       <Columns> 


                                <asp:BoundField DataField="cname" HeaderText="Client Name" ReadOnly="true" SortExpression="cname"  />
                                <asp:BoundField DataField="bname" HeaderText="Branch" ReadOnly="true" SortExpression="bname" />
                                <asp:BoundField DataField="name" HeaderText="Contact " SortExpression="name" />
                                 <asp:BoundField DataField="type" HeaderText="Type " SortExpression="type" ReadOnly="true" />
                                  <asp:BoundField DataField="description" HeaderText="Description " SortExpression="description" ReadOnly="true" />



                                <asp:CommandField ShowEditButton="true" />
                                 <asp:TemplateField>
                               <ItemTemplate>
                                          <asp:linkbutton id="ContactlnkDelete" runat="server" text="Delete" causesvalidation="false" commandname="Delete" commandargument="ID">
                                            </asp:linkbutton>


                                            <cc1:modalpopupextender id="lnkDelete_ModalPopupExtender2" runat="server" cancelcontrolid="ButtonDeleteCancel" okcontrolid="ButtonDeleleOkay" 
                                                targetcontrolid="ContactlnkDelete" popupcontrolid="DivDeleteConfirmation" backgroundcssclass="ModalPopupBG">
                                           </cc1:modalpopupextender>
                                            <cc1:confirmbuttonextender id="lnkDelete_ConfirmButtonExtender2" runat="server" targetcontrolid="ContactlnkDelete" enabled="True" 
                                                    displaymodalpopupid="lnkDelete_ModalPopupExtender2">
                                           </cc1:confirmbuttonextender>
                                    </ItemTemplate>

                               </asp:TemplateField>
                            </Columns>


                       <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                       <RowStyle BackColor="#EFF3FB" />
                       <EditRowStyle BackColor="#2461BF" />
                       <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                       <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                       <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                       <AlternatingRowStyle BackColor="White" />
               </asp:GridView> 

但它也失败了。拜托,我需要你的协助。

3 个答案:

答案 0 :(得分:2)

您必须使用 DataBound 代替 RowDataBound ,如下所示:

protected void gvList_DataBound(object sender, EventArgs e)
{
    string oldValue = string.Empty;
    string newValue = string.Empty;
    for (int j = 0; j < 2; j++)
    {
        for (int count = 0; count < gvList.Rows.Count; count++)
        {
            oldValue = gvList.Rows[count].Cells[j].Text;
            if (oldValue == newValue)
            {
                gvList.Rows[count].Cells[j].Text = string.Empty;
            }
            newValue = oldValue;
        }
    }
}

<强>更新

如果你想删除行:

替换:

gvList.Rows[count].Cells[j].Text = string.Empty;

使用:

gvList.Rows[count].Visible = false;

答案 1 :(得分:0)

您正在为newValue和OldValue分配相同的值,并且“必须使用RowDataBound的Page_Load事件内容”(感谢Fer指出我错过了什么。)

试试这个:

 for (int column = 0; column < 2; column++)
    {
        for (int row = 0; row < gvList.Rows.Count; row++)
        {       

            if(gvList.Rows[row].Cells[column].Text != "")
                oldValue = gvList.Rows[row].Cells[column].Text;

            if (oldValue == newValue)
            {
                gvList.Rows[row].Cells[column].Text = string.Empty;
            }       

            if(row+1 < gvList.Rows.Count)
             newValue = gvList.Rows[row+1].Cells[column].Text;
        }
    }

答案 2 :(得分:0)

在上面的代码中,我使用了“ for”循环,其中位置“ i”的行与所有位置“ j”的行进行比较,如果存在匹配,则位置“ i”的行按顺序消除避免重复数据

   void delete_Duplicate_data()
    {
        for (int i = 0; i < Gv.RowCount; i++)
        {
            for (int j = i + 1; j < Gv.RowCount; j++)
            {
               if (Gv.Rows[i].Cells[gID.Name].Value.ToString() == Gv.Rows[j].Cells[gID.Name].Value.ToString())
                {
                     Gv.Rows.Remove(Gv.Rows[i]);
                }
            }
        }
    }