Eg:
//In a row
GridView1.Rows[i].CssClass = "row_style";
//In a cell
GridView1.Rows[i].Cells[j].CssClass = "cel_style";
Here “i” and “j” are int numbers.
But in a GridView TemplateField , you can’t change the attributes of a control that way.
So in the fallowing example I have shown you how to do that.
In this example I have changed the “CssClass” attribute of an ASP button control.
Sample Grid view :
<asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Age" HeaderText="Age" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnRemove" Text="Remove" CommandName="btnRemove_Click" CommandArgument="<%#Eval("Age")%>"
runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
In “RowDataBound” event you can change the attributes of the control that you want…
//Code
GridView1.DataSource = dt; // dt is a DataTable
GridView1.RowDataBound += new GridViewRowEventHandler(GridView1_DataBound);
GridView1.DataBind();
//Code
/// <summary>
/// Handles the DataBound event of the GridView1 control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewRowEventArgs"/> instance containing the event data.</param>
private void GridView1_DataBound(object sender, GridViewRowEventArgs e)
{
Button btnRemove = (Button)e.Row.Cells[2].FindControl("btnRemove");
if (btnRemove != null)
{
btnRemove.CssClass = "btn_73_dis";
}
}