Reputation: 126
I'm trying to add a custom row on every 25th row. (25, 50, 75...) The custom row should be like
Essentially mimicking the example gridview's format below. The reason for this is to quickly add a customer and do something with them. Moving forward once the information is inside the textboxes won't be an issue.
Using:
I've come across a lot of different examples for doing this, but none where I add a row that's different from the item templates that are already there. In this case having a textbox on the 25th row instead of a label. I'm not sure if this is possible or where to begin with this.
<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>
The following code gathers the data that is needed and populates the 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();
}
Upvotes: 1
Views: 485
Reputation: 35514
This can be done in the RowCreated
event of a GridView. This works best with AutoGenerateColumns
set to 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++;
}
Upvotes: 1