2

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 2

6

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);
     }
 }
Sign up to request clarification or add additional context in comments.

Comments

-2

create a method to add columns in user control, and keep it accessor public. Now call that function from the aspx page where you have you that control object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.