I have a gridview in a user control. I am using BoundField for displaying columns in gridview in aspx page. Can I add additional columns from code behind file (.cs)? I need to add few additional columns in the user control is used in different page.
2 Answers
You can add a new cell in RowDataBound event of the gridview, like below. (I have added the comments where needed)
protected void grd_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
TableHeaderCell NewCell = new TableHeaderCell();
NewCell.Text = "Header Text";
e.Row.Cells.AddAt(4(Index of Cell where you want to add cell), NewCell);
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
TableCell NewCell= new TableCell();
NewCell.ID = "NewCell";
NewCell.Text = "Text value of cell which you want to display";
e.Row.Cells.AddAt(4, NewCell);
}
}