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.
<html xmlns="">
<head runat="server">
    <title>Change GridView row color based on condition in ASP.NET using C#/VB.NET</title>
    <form id="form1" runat="server">
  <asp:GridView runat ="server" ID="grd" AutoGenerateColumns ="false"
            style="margin-bottom: 35px" Width="550px" >
    <asp:TemplateField HeaderText ="ID">
    <asp:Label ID="lblId" Text='<%#Eval("id") %>' runat="server"></asp:Label>
     <asp:TemplateField HeaderText ="First Name">
    <asp:Label ID="lblFname" Text='<%#Eval("FirstName") %>' runat="server"></asp:Label>
     <asp:TemplateField HeaderText ="Last Name">
    <asp:Label ID="lblLname" Text='<%#Eval("LastName") %>' runat="server"></asp:Label>
     <asp:TemplateField HeaderText ="Address">
    <asp:Label ID="lblAddr" Text='<%#Eval("Address") %>' runat="server"></asp:Label>

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();
        grd.DataSource = ds.Tables[0];

        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;


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()
        grd.DataSource = ds.Tables(0)

        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

    End Sub
End Class

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

"If you like my blog or articles, you can appreciate by leaving your comments or Liking my Facebook page Aspdotnet-kishore, following on Google+ Aspdotnet-Kishore, Twitter on AspdotnetKishore, Linked in Aspdotnet-Kishore, stumbling my posts on stumble upon and subscribing on RSSfeed Aspdotnet-Kishore for free updates directly to your Email inbox . Watch my blog for more articles."

1 comment:

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