Reputation: 1472
I have a page where I need to create a view where we can add rows and that view needs to look like this.
Here the second row has a big textbox. On filling the data and clicking Add new link the row should be saved in DB and be visible, and a new footer row should appear to fill the new data. I went thru google and SO and found ListView, DataList but I couldn't figure out a way to achive this. I know this question may be a duplicate. But I wanted to show what I need through screenshot.
Please help me a little more and guide to correct direction. Thanks.
Upvotes: 0
Views: 617
Reputation: 35544
You can do this in the RowCreated
event of the GridView. However the extra rows will be placed above the normal header and footer rows. In here you can also add controls to the extra rows if you want. But keep in mind that dynamically created controls have to be recreated on every PostBack, so databinding must not be inside the IsPostBack
check.
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
//cast the sender back to a gridview
GridView gv = sender as GridView;
//check if the row is the header row
if (e.Row.RowType == DataControlRowType.Header)
{
//create a new row
GridViewRow extraHeader = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
extraHeader.BackColor = Color.Red;
//loop all the columns and create a new cell for each
for (int i = 0; i < gv.Columns.Count; i++)
{
TableCell cell = new TableCell();
cell.Text = "ExtraHeader " + i;
//add the cell to the new header row
extraHeader.Cells.Add(cell);
}
//add the new row to the gridview
gv.Controls[0].Controls.AddAt(0, extraHeader);
}
//check if the row is the footer row
if (e.Row.RowType == DataControlRowType.Footer)
{
//create a new row
GridViewRow extraFooter = new GridViewRow(0, 0, DataControlRowType.Footer, DataControlRowState.Insert);
extraFooter.BackColor = Color.Green;
//add one cell with colspan 2
TableCell cell1 = new TableCell();
cell1.Text = "ExtraFooter 1";
cell1.ColumnSpan = 2;
extraFooter.Cells.Add(cell1);
//add another one with colspanning the rest
TableCell cell2 = new TableCell();
cell2.Text = "ExtraFooter 2";
cell2.ColumnSpan = gv.Columns.Count - 2;
extraFooter.Cells.Add(cell2);
//add +2 to the row count. one for the extra header and 1 for the extra footer
int insertIndex = gv.Rows.Count + 2;
//add the new row to the gridview
gv.Controls[0].Controls.AddAt(insertIndex, extraFooter);
}
}
Upvotes: 1