How to use RowDataBound event in GridView

GridView RowDataBound Event
GridView is an server control. GridView display the values of a data source in a table. in a GridView each column represents a field and each row represents a record from data source. gridview server control allow 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 render. RowDataBound event provide an event handling method that performs a custom routine. developers can modify the values of specific row when this event occurs. we also can format specific row 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 allow 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, footer row.

RowDataBound property provide a way to developers to edit, format or design a specific cell or row in gridview control when row data 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 which specific filed's value greater than or lower than a specified number, or we can apply a different row color which specified cell value in a range etc.

<%@ 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");
                e.Row.ForeColor = System.Drawing.Color.Crimson;
                e.Row.Font.Italic = true;
                e.Row.BackColor = System.Drawing.Color.LightPink;

<html xmlns="">
<head id="Head1" runat="server">
    <title> GridView RowDataBound Event example: how to format specific row</title>
    <form id="form1" runat="server">
        <h2 style="color:Navy">GridView OnRowDataBound Example</h2>
            ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            SelectCommand="SELECT ProductID, ProductName, UnitPrice FROM Products"
Related examples