0

我有三个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()` 

我的girdview如下。

 <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> 

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

4

3 回答 3

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;
于 2013-09-27T08:48:57.423 回答
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;
        }
    }
于 2013-09-27T08:31:48.577 回答
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]);
                }
            }
        }
    }
于 2020-05-11T15:26:44.823 回答