【问题标题】:Adding unique row into gridview after every x rows每 x 行后将唯一行添加到 gridview
【发布时间】:2017-10-13 16:36:06
【问题描述】:

简介

我正在尝试在每 25 行添加一个自定义行。 (25, 50, 75...) 自定义行应该是这样的

  • (开始销售按钮)(创建客户 按钮)(文本框)(文本框)(文本框)(文本框)(文本框)

基本上模仿了下面示例 gridview 的格式。这样做的原因是为了快速添加客户并与他们做一些事情。一旦信息在文本框内,继续前进将不是问题。

使用:

  1. ASP.net
  2. C#

问题

我遇到了很多不同的示例来执行此操作,但是我添加的行与已经存在的项目模板不同的地方都没有。在这种情况下,第 25 行有一个文本框而不是标签。我不确定这是否可行或从哪里开始。

gridview的声明

<asp:GridView ID="grdCustomersSearched" runat="server" AutoGenerateColumns="false" OnRowCommand="grdCustomersSearched_RowCommand" AllowPaging="True" PageSize="25" OnPageIndexChanging="grdCustomersSearched_PageIndexChanging" >
            <Columns>
                <asp:TemplateField HeaderText="Sale">
                    <ItemTemplate>
                        <asp:LinkButton ID="lbtnStartSale" CommandName="StartSale" CommandArgument='<%#Eval("CustomerId") %>' Text="Start Sale" runat="server">Start Sale</asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="View Profile">
                    <ItemTemplate>
                        <asp:LinkButton ID="lbtnViewCustomer" CommandName="ViewProfile" CommandArgument='<%#Eval("CustomerId") %>' Text="View Profile" runat="server">View Profile</asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Customer Number">
                    <ItemTemplate>
                        <asp:Label runat="server" Text='<%#Eval("CustomerId") %>' ID="key"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Customer Name">
                    <ItemTemplate>
                        <asp:Label runat="server" Text='<%#Eval("firstName") + " " + Eval("lastName") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Customer Address">
                    <ItemTemplate>
                        <asp:Label runat="server" Text='<%#Eval("primaryAddress") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Phone Number">
                    <ItemTemplate>
                        <asp:Label runat="server" Text='<%#Eval("primaryPhoneNumber") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="City">
                    <ItemTemplate>
                        <asp:Label runat="server" Text='<%#Eval("city") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <EmptyDataTemplate>
                No current customer data, please search for a customer
            </EmptyDataTemplate>
        </asp:GridView>

用于填充 gridview 的代码隐藏

以下代码收集所需的数据并填充网格视图。

protected void btnCustomerSearch_Click(object sender, EventArgs e)
    {            
        //Looks through database and returns a list of customers
        //based on the search criteria entered
        SweetShopManager ssm = new SweetShopManager();
        c = ssm.GetCustomerfromSearch(txtSearch.Text);
        //Binds the results to the gridview
        grdCustomersSearched.Visible = true;
        grdCustomersSearched.DataSource = c;
        grdCustomersSearched.DataBind();            
    }   

网格视图示例

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    这可以在 GridView 的 RowCreated 事件中完成。这在 AutoGenerateColumns 设置为 false 时效果最佳。

    int rowIndex = 0;
    
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        //the amount of rows in between the inserted rows
        int rowsInBetween = 3;
    
        //check if the row is a datarow
        if (e.Row.RowType == DataControlRowType.DataRow && rowIndex % (rowsInBetween + 1) == 0 && rowIndex > 0)
        {
            //create a new row
            GridViewRow extraRow = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);
            extraRow.BackColor = Color.Green;
    
            //add one cell with text
            TableCell cell1 = new TableCell();
            cell1.Text = "ExtraRow";
            extraRow.Cells.Add(cell1);
    
            //add another one with a textbox and column spanning
            TableCell cell2 = new TableCell();
            cell2.ColumnSpan = GridView1.Columns.Count - 1;
            TextBox tb1 = new TextBox();
            tb1.Text = "Inserted TextBox";
            cell2.Controls.Add(tb1);
            extraRow.Cells.Add(cell2);
    
            //add the new row to the gridview
            GridView1.Controls[0].Controls.AddAt(rowIndex, extraRow);
    
            //extra increment the row count
            rowIndex++;
        }
    
        rowIndex++;
    }
    

    【讨论】:

    • 谢谢。这几乎正​​是我想要的。不过,我还有另一个问题。由于我在 gridview 上使用分页并将其限制为 25 条数据,如果我将 rowsInBetween 设置为 24,它就在那里,但我后面有一行数据。如果我将其设置为 25,则它不再存在。是否可以将其显示为最后一行而不是数据,还是分页有问题?
    • 那为什么不使用FooterTemplate呢?然后你甚至不需要以编程方式添加行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-05
    • 1970-01-01
    相关资源
    最近更新 更多