connectseekers.com

Friday, 1 August 2014

Gridview Row Highlighting in Condition Bases in Asp.net Using C#.Net

8/01/2014 - By Pranav Singh 0

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.


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--="">";
        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.


About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com
Email Id : contact@aspdotnet-pools.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014
back to top