在每x行之后将唯一行添加到gridview中

时间:2017-10-13 16:36:07

标签: c# asp.net gridview

简介

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

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

基本上模仿下面的示例gridview格式。这样做的原因是快速添加客户并对其进行操作。一旦信息在文本框内,向前移动将不会成为问题。

使用:

  1. ASP.net
  2. C#
  3. 问题

    我为此做了很多不同的例子,但是我没有添加与已经存在的项目模板不同的行。在这种情况下,在第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的代码隐藏

    以下代码收集所需的数据并填充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();            
        }   
    

    gridview的示例

    Example of the gridview

1 个答案:

答案 0 :(得分: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++;
}