views:

99

answers:

3

Hi All, 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

A: 

you need to use gridview rowdatabound event for that like

protected void yourGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Label lblArea = e.Row.FindControl("Yourlbl") as Label;
        lblArea.BorderColor// set here your style what ever you want.
    }
}
Muhammad Akhtar
+2  A: 

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.

John
yep need to restyle whole columns, just looking at http://aspadvice.com/blogs/joteke/archive/2007/02/19/Access-GridView-BoundFields-with-_2200_field-name_2200_.aspx which is mentioned on the link you included. Thanks, will update once I've given it a whirl :)
Alex
A: 

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("");
}
Bootcamp