Monday 21 October 2013

Change GridView row color based on condition in ASP.NET using C#/VB.NET

Hi friends, in this article I will explain about Change GridView row color based on condition in ASP.NET using C#/VB.NET.
In ASP.NET:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Change GridView row color based on condition in ASP.NET using C#/VB.NET</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
  <asp:GridView runat ="server" ID="grd" AutoGenerateColumns ="false"
            style="margin-bottom: 35px" Width="550px" >
    <Columns>
    <asp:TemplateField HeaderText ="ID">
    <ItemTemplate>
    <asp:Label ID="lblId" Text='<%#Eval("id") %>' runat="server"></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
     <asp:TemplateField HeaderText ="First Name">
    <ItemTemplate>
    <asp:Label ID="lblFname" Text='<%#Eval("FirstName") %>' runat="server"></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
     <asp:TemplateField HeaderText ="Last Name">
    <ItemTemplate>
    <asp:Label ID="lblLname" Text='<%#Eval("LastName") %>' runat="server"></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
     <asp:TemplateField HeaderText ="Address">
    <ItemTemplate>
    <asp:Label ID="lblAddr" Text='<%#Eval("Address") %>' runat="server"></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>
    </div>
    </form>
</body>
</html>

In C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class ChangeRowColour : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["con"].ConnectionString);
        SqlCommand cmd = new SqlCommand("SELECT * FROM STUDENTS ORDER BY Firstname ASC", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        grd.DataSource = ds.Tables[0];
        grd.DataBind();

        foreach (GridViewRow row in grd.Rows)
        {
            Label lbl_Addr = (Label)row.FindControl("lblAddr");
            if (string.IsNullOrEmpty(lbl_Addr.Text))
            {
                row.BackColor = System.Drawing.Color.Pink;
            }
        }

    }
}


In VB.NET:
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data
Imports System.Data.SqlClient

Partial Public Class ChangeRowColour
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(sender As Object, e As EventArgs)
        Dim con As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("con").ConnectionString)
        Dim cmd As New SqlCommand("SELECT * FROM STUDENTS ORDER BY Firstname ASC", con)
        Dim da As New SqlDataAdapter(cmd)
        Dim ds As New DataSet()
        da.Fill(ds)
        grd.DataSource = ds.Tables(0)
        grd.DataBind()

        For Each row As GridViewRow In grd.Rows
            Dim lbl_Addr As Label = DirectCast(row.FindControl("lblAddr"), Label)
            If String.IsNullOrEmpty(lbl_Addr.Text) Then
                row.BackColor = System.Drawing.Color.Pink
            End If
        Next

    End Sub
End Class

The output of the above code as shown in the below figure.

1 comment:

© 2012-2018 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.