GridView RowDataBound Event
GridView is an asp.net server control. GridView displays the values of a
data source in a table. in a GridView each column represents a field and each
row represents a record from the data source. GridView server control allows
us to select, sort, and edit data.
GridView RowDataBound event occurs when a data row is bound to data in a GridView server control. Each row in the GridView must be bound to record in the data source before GridView control renders. RowDataBound event provides an event-handling method that performs a custom routine. Asp.net developers can modify the values of the specific row when this event occurs. We also can format specific rows in GridView using this event.
GridViewRow object represents an individual row in GridView server control. GridViewRow.RowType property gets the row type of the GridViewRow object. RowType property can determine the type of row that the GridViewRow object represents. The different row types are DataRow, Footer, Header, EmptyDataRow, Pager, and Separator.
A GridViewRowEventArgs object is passed to the event handling method. This object allows us to access the properties of the row being bound. We can access a specific Cell in the row and can access a specific row in the GridView. We can also determine which row type is being bound such as header row, data row, or footer row.
RowDataBound property provides a way to asp.net developers to edit, format, or design a specific cell or row in GridView control when row data is bound. We also can format specific rows or cells in GridView by predefined conditions. Such as we can apply bold text for cells or rows whose specific field's value is greater than or lower than a specified number, or we can apply a different row color which a specified cell value in a range, etc.
GridView RowDataBound event occurs when a data row is bound to data in a GridView server control. Each row in the GridView must be bound to record in the data source before GridView control renders. RowDataBound event provides an event-handling method that performs a custom routine. Asp.net developers can modify the values of the specific row when this event occurs. We also can format specific rows in GridView using this event.
GridViewRow object represents an individual row in GridView server control. GridViewRow.RowType property gets the row type of the GridViewRow object. RowType property can determine the type of row that the GridViewRow object represents. The different row types are DataRow, Footer, Header, EmptyDataRow, Pager, and Separator.
A GridViewRowEventArgs object is passed to the event handling method. This object allows us to access the properties of the row being bound. We can access a specific Cell in the row and can access a specific row in the GridView. We can also determine which row type is being bound such as header row, data row, or footer row.
RowDataBound property provides a way to asp.net developers to edit, format, or design a specific cell or row in GridView control when row data is bound. We also can format specific rows or cells in GridView by predefined conditions. Such as we can apply bold text for cells or rows whose specific field's value is greater than or lower than a specified number, or we can apply a different row color which a specified cell value in a range, etc.
GridViewFormattingSpecificRow.aspx
<%@ Page Language="C#" %>
<!DOCTYPE html>
<script runat="server">
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {
if(e.Row.RowType == DataControlRowType.DataRow)
{
Decimal ProductPrice = (decimal)DataBinder.Eval(e.Row.DataItem, "UnitPrice");
if(ProductPrice<20)
{
e.Row.ForeColor = System.Drawing.Color.Crimson;
e.Row.Font.Italic = true;
e.Row.BackColor = System.Drawing.Color.LightPink;
}
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>asp.net GridView RowDataBound Event example: how to format specific row</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Navy">GridView OnRowDataBound Example</h2>
<asp:SqlDataSource
ID="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT ProductID, ProductName, UnitPrice FROM Products"
>
</asp:SqlDataSource>
<asp:GridView
ID="GridView1"
runat="server"
DataSourceID="SqlDataSource1"
ForeColor="AliceBlue"
BackColor="DarkSalmon"
BorderColor="Salmon"
HeaderStyle-BackColor="Crimson"
AllowPaging="true"
AutoGenerateColumns="true"
DataKeyNames="ProductID"
OnRowDataBound="GridView1_RowDataBound"
>
</asp:GridView>
</div>
</form>
</body>
</html>