This article will show you how you can highlight the
gridview row on the bases of conditional data of a row in asp.net using c#.net.
This we have performed on rowdatabound event of gridview.
Some of my previous articles are as follows: Binding
Gridview By Access DataBase Using C#.Net in Asp.Net, Code
to Export GridView to PDF in Asp.Net Using C# and Vb.Net, Drag
Drop Cells in GridView Control Using Asp.net C# and jQuery, Highlight
GridView Row on Mouseover Using CSS in Asp.Net C#.Net, How
to Bind Data To TextBox Values in Asp.Net GridView Control Using C#.Net.
So for this article first we will create a new asp.net
application and ad a gridview control on page. After adding the control your
code will look as shown below.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ProjectDemo_Asp.et.Default"
%>
<!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>Gridview
Row Highlighting in Condition Bases in Asp.net Using C#.Net</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="Label9" runat="server" Text="" style="font-weight:bold;color:Red;"></asp:Label>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" EmptyDataText="There are no data records to display."
Width="100%" BorderStyle="Solid" ShowFooter="True"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#
Eval("source_numb") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TITLE">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("title")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="PUBLISH YEAR">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%#
Bind("publication_year") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="PRICE">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%#
Bind("retail_price") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#66CCFF" />
</asp:GridView>
</form>
</body>
</html>
|
Now we will check the .cs page code
using System;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace ProjectDemo_Asp.et
{
public partial class Default : System.Web.UI.Page
{
public
string connectionstring = "<--your connsction="" string--="">"--your>;
protected
void Page_Load(object
sender, EventArgs e)
{
if
(!IsPostBack)
{
GetDataFromDataBase();
}
}
///
/// Function for binding retribing the data from database
///
public void GetDataFromDataBase()
{
DataTable _objdt = new DataTable();
string querystring = "select * from Books;";
SqlConnection _objcon = new SqlConnection(connectionstring);
SqlDataAdapter _objda = new SqlDataAdapter(querystring, _objcon);
_objcon.Open();
_objda.Fill(_objdt);
if (_objdt.Rows.Count > 0)
{
GridView1.DataSource = _objdt;
GridView1.DataBind();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var bookPrice = (Label)e.Row.FindControl("Label4");
if (Convert.ToDecimal(bookPrice.Text) > 12)
{
e.Row.BackColor = System.Drawing.Color.Red;
}
}
}
}
}
|
In above code we have check the row value on rowdatabound and if the control is greater then 12 on that
case we are highlighting that row.
So here is the code
protected
void GridView1_RowDataBound(object sender, GridViewRowEventArgs
e)
{
if
(e.Row.RowType == DataControlRowType.DataRow)
{
var
bookPrice = (Label)e.Row.FindControl("Label4");
if
(Convert.ToDecimal(bookPrice.Text) >
12)
{
e.Row.BackColor =
System.Drawing.Color.Red;
}
}
}
|
Now we have done run the page.
0 comments:
Please let me know your view