Part - 8
Working with GridView Control 
Events
The GridView 
control includes a rich set of events that we can handle to customize the 
control's behavior and appearance. These events can be divided into three 
groups.
First, the GridView control supports the following set of events that are 
raised when the control displays its rows:
- 
DataBinding 
Raised immediately before the GridView is bound to its data source.
- 
DataBound 
Raised immediately after a GridView is bound to its data source.
- 
RowCreated 
Raised when each row in the GridView is created.
- 
RowDataBound 
Raised when each row in the GridView is bound to data.
Second, the 
GridView control includes the following set of events that are raised when 
we are editing records:
- 
RowCommand 
Raised when an event is raised by a control contained in the GridView.
- 
RowUpdating 
Raised immediately before a GridView updates a record.
- 
RowUpdated 
Raised immediately after a GridView updates a record.
- 
RowDeleting 
Raised immediately before a GridView deletes a record.
- 
RowDeleted 
Raised immediately after a GridView deletes a record.
- 
RowCancelingEdit Raised when we cancel updating a record.
Finally, the 
GridView control supports the following events related to sorting, 
selecting, and paging:
- 
PageIndexChanging Raised immediately before the current page is changed.
- 
PageIndexChanged Raised immediately after the current page is changed.
- 
Sorting Raised 
immediately before sorting.
- 
Sorted Raised 
immediately after sorting.
- 
SelectedIndexChanging Raised immediately before a row is selected.
- 
SelectedIndexChanged Raised immediately after a row is selected.
Using Highlighting GridView Rows
Introduction & Demonstration
Imagine that we want to highlight particular rows in a GridView. For 
example, when displaying a table of marks totals, we might want to highlight 
the rows in which the marks are greater than a certain amount.
![grid9.gif]()
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD 
XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e AsGridViewRowEventArgs)
        If e.Row.RowType 
= DataControlRowType.DataRow Then
            Dim TOTAL_MARKS As Decimal = CType(DataBinder.Eval(e.Row.DataItem,"TOTAL_MARKS"), Decimal)
            If TOTAL_MARKS 
> 450 Then
                e.Row.BackColor = System.Drawing.Color.Blue
            End If
        End If
    End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <style type="text/css">
    </style>
</head>
<body>
    <form id="form1" runat="server" >
    <div>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataSourceID="SqlDataSource1" 
            EmptyDataText="There 
are no data records to display." Width="262px"OnRowDataBound="GridView1_RowDataBound">
            <Columns>
                <asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" />
                <asp:BoundField DataField="NAME" HeaderText="NAME" SortExpression="NAME" />
                <asp:BoundField DataField="TOTAL_MARKS" HeaderText="TOTAL_MARKS 
(out of 500)" 
                    SortExpression="TOTAL_MARKS" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString1 %>" 
            ProviderName="<%$ ConnectionStrings:DatabaseConnectionString1.ProviderName %>" 
            SelectCommand="SELECT 
[ID], [NAME], [TOTAL_MARKS] FROM [MARKSHEET]">
        </asp:SqlDataSource>
     </div>
    </form>
</body>
</html>
In above figure, the GridView1_RowDataBound() method is executed when the 
GridView renders each of its rows. The second parameter passed to this event 
handler is an instance of the GridViewRowEventArgs class. This class exposes 
a GridViewRow object that represents the row being bound.
The GridViewRow object supports several useful properties, here is some of 
them:
- 
Cells 
Represents the collection of table row cells associated with the row 
being bound.
- 
DataItem 
Represents the data item associated with the row being bound.
- 
DataItemIndex 
Represents the index of the data item in its DataSet associated with the 
row being bound.
- 
RowIndex 
Represents the index of the row being bound.
- 
RowState 
Represents the state of the row being bound. Possible values are 
Alternate, Normal, Selected, Edit. Because these values can be combined 
(for example, the RowState can be Alternate Edit), use a bitwise 
comparison with RowState.
- 
RowType 
Represents the type of row being bound. Possible values are DataRow, 
Footer, Header, NullRow, Pager, Separator.
In above list, the 
RowType property we have used to verify that the row is a DataRow. The 
DataItem property is used to retrieve the database record associated with 
the row. Notice that the DataBinder.Eval() method is used to retrieve the 
value of the TOTAL_MARKS.
Note: Continue 
in Next Part.
HAVE A GREAT CODING!