Introduction:
In this article,i am going to explain about how to format or design a gridview rows using available gridview properties.
Main:
we can format the GridView rows as per your requirements.For ex,if you wants to set a property for alternating row,
AlternatingRowStyle-BackColor =”Blue”
If you wants to format a gridview row at runtime the OnRowCreated is the best option,
protected void OnRowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (((e.Row.RowIndex + 1)) == 5)
{
e.Row.BackColor = System.Drawing.Color.Red;
}
}
}
protected void OnRowCreated(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { if (((e.Row.RowIndex + 1)) == 5) { e.Row.BackColor = System.Drawing.Color.Red; } } } |
if you wants to set some style in onmouseover and onmouseout then,
protected void OnRowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover",
"this.style.backgroundColor='Blue'");
e.Row.Attributes.Add("onmouseout",
"this.style.backgroundColor='skyBlue'");
}
}
protected void OnRowCreated(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='Blue'"); e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='skyBlue'"); } } |
if you wants to display the gridview row data into alert then,
protected void OnRowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int rowIndex = e.Row.DataItemIndex;
rowIndex += 1;
e.Row.Attributes.Add("onClick", "alert('I am gridview row and my data is :
" + rowIndex.ToString()+ "')");
}
}
protected void OnRowCreated(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { int rowIndex = e.Row.DataItemIndex; rowIndex += 1; e.Row.Attributes.Add("onClick", "alert('I am gridview row and my data is : " + rowIndex.ToString()+ "')"); } } |
Sometimes we need to foramt the display some of the fields,for ex date,
DataFormatString=”{0:d}”
For Currency,
DataFormatString=”{0:C}”
Conclusion:
Hope this helps,
Happy Coding.
Great information! I’ve been looking for something like this for a while now. Thanks!
great post.
My cousin recommended this blog and she was totally right keep up the fantastic work!
Valuable info. Lucky me I found your site by accident, I bookmarked it.