Suppose that we want to highlight particular rows in GridView. For example, when
displaying a table of Salary totals, we might want to highlight the rows in which the Salary
are greater than a certain amount.
For example, A GridView control that displays a summary column representing the total of all Employee Salaries.
We can modify the appearance of individual rows in a GridView control by handling
the RowDataBound event.The grid_RowDataBound() method is executed when GridView
renders each of its rows (including its header and footer). 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 (this is not a complete list):
Sr.No |
Property |
Description |
1) |
Cells |
Represents the collection of table row cells associated with the row being bound |
2) |
DataItem |
Represents the data item associated with the row being bound. |
3) |
DataItemIndex |
Represents the index of the data item in its DataSet associated with the row being bound. |
4) |
RowIndex |
Represents the index of the row being bound |
5) |
RowState |
Represents the state of the row being bound. Possible values are
Alternate, Normal, Selected, and Edit. Because these values can be combined (for example, RowState can be Alternate Edit), use a bitwise comparison RowState |
6) |
RowType |
Represents the type of row being bound. Possible values are DataRow,Footer, Header, NullRow, Pager, and Separator |
The RowType property verifies that the row is DataRow (not a header row
or some other type of row). The DataItem property retrieves the database record associated
with the row. The DataBinder.Eval() method retrieves the value of the Salary.
Code: GridView16_HighLigthRow.aspx.cs
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridView16_HighLigthRow.aspx.cs"
Inherits="LearnAsp.Net.ControlDemo.GridView.GridView16_HighLigthRow" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Highlighting Row</title>
</head>
<body>
<form ID="form1" runat="server">
<div>
<h1>
Employee Info</h1>
< asp:GridView ID="grid" runat="server" AutoGenerateColumns="false" CellPadding="4"
OnRowDataBound="grid_RowDataBound"
ForeColor="#333333">
<Columns>
<asp:BoundField DataField="empno" HeaderText="Emp No" />
<asp:BoundField DataField="ename" HeaderText="Name" />
<asp:BoundField DataField="sal" HeaderText="Salary" DataFormatString="{0:c}" HtmlEncode="false" />
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<EditRowStyle BackColor="#999999" />
< SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
< PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
< HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
< AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</ asp:GridView >
<br />
</div>
</form>
</body>
</html>
Code: GridView16_HighLigthRow.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OracleClient;
using System.Data;
using System.Configuration;
namespace LearnAsp.Net.ControlDemo.GridView
{
public partial class GridView16_HighLigthRow : System.Web.UI.Page
{
static string strConn = ConfigurationManager.ConnectionStrings["LearnAsp"].ToString();
private OracleConnection objConn = new OracleConnection(strConn);
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) { GridBind(); }
}
protected void GridBind()
{
String query = " select * from emp ";
OracleCommand objCmd= new OracleCommand(query, objConn);
DataTable dt = new DataTable();
objConn.Open();
OracleDataAdapter oda = new OracleDataAdapter(objCmd);
oda.Fill(dt);
objConn.Close();
if (dt.Rows.Count > 0)
{
grid.DataSource = dt;
grid.DataBind();
}
else
{
grid.DataSource = dt.NewRow();
grid.DataBind();
int columncount = grid.Rows[0].Cells.Count;
grid.Rows[0].Cells.Clear();
grid.Rows[0].Cells.Add(new TableCell());
grid.Rows[0].Cells[0].ColumnSpan = columncount;
grid.Rows[0].Cells[0].Text = "No Records Found";
}
}
protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
decimal Salary = (decimal)DataBinder.Eval(e.Row.DataItem, "sal");
if (Salary > 1500)
e.Row.BackColor = System.Drawing.Color.Yellow;
}
}
}
}
Output:
No comments:
Post a Comment
Please do not enter any spam link in the comment box.