0

I'm creating a dynamic GridView from a DataTable that is returned from a stored procedure. I call and bind with the following code:

DataTable dt = Sql.reportData(Convert.ToInt32(Session["userID"]));
this.GridView1.DataSource = dt.DefaultView;
this.GridView1.DataBind();

I need to restyle certain columns but they are not always the same column number, and only have the headers text string to identify it. Is there an easy way to track a column down like this so I can edit its attributes?

Thanks, Alex

2 Answers 2

2

I've run into this myself. You've got to loop through the column names, get the index, and then refer to the index to manipulate the style.

Muhammad is right about the timing, but you won't be searching for a label--it seems you want to style the entire column, right?

http://forums.asp.net/p/1076872/1584635.aspx

the above has several versions of a solution.

Sign up to request clarification or add additional context in comments.

1 Comment

yep need to restyle whole columns, just looking at aspadvice.com/blogs/joteke/archive/2007/02/19/…2200_field-name_2200.aspx which is mentioned on the link you included. Thanks, will update once I've given it a whirl :)
0

The best place to find the control and use it will be in the RowCreated event. RowDataBound should not be used because you dont have to manipulate the data with which the column is being binded. So restyle the elements in the column by searching them in the RowCreated event.

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
  e.Row.FindControl("");
} 

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.