Part - 8
Working with GridView Control
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
First, the GridView control supports the following set of events that are
raised when the control displays its rows:
Raised immediately before the GridView is bound to its data source.
Raised immediately after a GridView is bound to its data source.
Raised when each row in the GridView is created.
Raised when each row in the GridView is bound to data.
GridView control includes the following set of events that are raised when
we are editing records:
Raised when an event is raised by a control contained in the GridView.
Raised immediately before a GridView updates a record.
Raised immediately after a GridView updates a record.
Raised immediately before a GridView deletes a record.
Raised immediately after a GridView deletes a record.
RowCancelingEdit Raised when we cancel updating a record.
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.
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD
XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e AsGridViewRowEventArgs)
= DataControlRowType.DataRow Then
Dim TOTAL_MARKS As Decimal = CType(DataBinder.Eval(e.Row.DataItem,"TOTAL_MARKS"), Decimal)
> 450 Then
e.Row.BackColor = System.Drawing.Color.Blue
<head id="Head1" runat="server">
<form id="form1" runat="server" >
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
are no data records to display." Width="262px"OnRowDataBound="GridView1_RowDataBound">
<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)"
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString1 %>"
ProviderName="<%$ ConnectionStrings:DatabaseConnectionString1.ProviderName %>"
[ID], [NAME], [TOTAL_MARKS] FROM [MARKSHEET]">
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
Represents the collection of table row cells associated with the row
Represents the data item associated with the row being bound.
Represents the index of the data item in its DataSet associated with the
row being bound.
Represents the index of the row being bound.
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.
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.
in Next Part.
HAVE A GREAT CODING!