Monday, 19 November 2012

Highlight Gridview Row When CheckBox is Checked in Asp.Net and C#

In this article I will explain how to highlight gridview rows when checkbox is checked or selected in asp.net using C#
ASPX page:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridviewCheckBox.aspx.cs"
    Inherits="GridviewCheckBox" %>

<!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></title>

   </head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="sm" runat="server" ></asp:ScriptManager>
    <asp:UpdatePanel runat="server" ID="up">
    <ContentTemplate>
    <div>
        <asp:GridView ID="gvBank" runat="server">
            <HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
            <Columns>
                <asp:TemplateField>
                    <HeaderTemplate>
                        <asp:CheckBox ID="chkHeader" runat="server"
                            oncheckedchanged="chkHeader_CheckedChanged"  AutoPostBack="true"/>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:CheckBox ID="chkChild" runat="server"
                            oncheckedchanged="chkChild_CheckedChanged" AutoPostBack="true" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
    </div>
      </ContentTemplate>
    </asp:UpdatePanel>
    </form>
</body>
</html>
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using System.Configuration;
using System.Data;

public partial class GridviewCheckBox : System.Web.UI.Page
{
    MySqlConnection con = new MySqlConnection(ConfigurationManager.AppSettings["ConnectionString"].ToString());
    MySqlCommand cmd = new MySqlCommand();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataSet ds = new DataSet();
            MySqlDataAdapter ada = new MySqlDataAdapter("SELECT Bank_Nam,STR_TO_DATE(cre_dt,'%m/%d/%Y %T') as CreatedDate,STR_TO_DATE(Mod_dt,'%m/%d/%Y %T') as ModifiedDate,Cre_by,Mod_by FROM bank  order by Bank_Nam", con);
            ada.Fill(ds);

            gvBank.DataSource = ds.Tables[0];
            gvBank.DataBind();
        }

    }
    protected void chkChild_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox checkbox = (CheckBox)sender;

        GridViewRow gdrow = (GridViewRow)checkbox.NamingContainer;
        if (((CheckBox)gdrow.FindControl("chkChild")).Checked)
            gdrow.BackColor = System.Drawing.Color.Red;
        else
            gdrow.BackColor = System.Drawing.Color.Transparent;

    }
    protected void chkHeader_CheckedChanged(object sender, EventArgs e)
    {
        foreach (GridViewRow rw in gvBank.Rows)
        {
            CheckBox chkBx = (CheckBox)rw.FindControl("chkChild");
            if (chkBx != null && chkBx.Checked)
                gvBank.BackColor = System.Drawing.Color.Transparent;
            else
                gvBank.BackColor = System.Drawing.Color.Gold;
        }


        
    }
}

No comments:

Post a Comment