How to Active-Inactive User in GridView in Asp.Net - ProgramIdea

How to Active-Inactive User in GridView in Asp.Net

Here we are binding a GridView to showing all user details and we can enable and disable user to block the user's login in the system.

We are using Image Button's CommandName Propery to Select then it works on SelectedIndexChanging / SelectedIndexChanged event, if you use CommandName as Update then it works on Rowupdating / RowUpdated event and if you use CommandName as Edit then it works on RowEditing event

Points Of Remember:

1. Fire GridView's event RowDataBound.

2. Fire GridView's event SelectedIndexChanging.

3. Add a ImageButton in GridView ItemTemplate.

4. Set ImageButton's CommandName propery to Select.

5. Set GridView's DataKeyNames Property to record ID (ID=primary/unique key of student table).

6. Add namespace System.Data and System.Data.SqlClient in your C# page.

<!DOCTYPE html>


<html xmlns="">

<head runat="server">

    <title>Active-Inctive User</title>



  <form id="form1" runat="server">


    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 

        OnRowDataBound="GridView1_RowDataBound" CellPadding="4"  DataKeyNames="ID"

        OnSelectedIndexChanging="GridView1_SelectedIndexChanging" ForeColor="#333333">

        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />       


        <asp:BoundField DataField="Name" HeaderText="Name" />

        <asp:BoundField DataField="Branch" HeaderText="Branch" />

        <asp:BoundField DataField="City" HeaderText="City" />

        <asp:BoundField DataField="Active" HeaderText="Active" />



        <asp:ImageButton ID="img_user" runat="server" CommandName="Select"

            ImageUrl='<%# Eval("Active" ) %>' Width="20px" Height="20px" />




        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />        

        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />






C# Codes :

using System.Data;

using System.Data.SqlClient;


public partial class test_Active : System.Web.UI.Page


    // sql connection

    SqlConnection con = new SqlConnection( @"Data Source=JITESH-PC\SQL;Initial

                                           Catalog=db_Student;Integrated Security=True");

    protected void Page_Load(object sender, EventArgs e)


        if (!IsPostBack)





    //method for binding GridView

    protected void BindGridView()


        DataTable dt = new DataTable ();        

        SqlDataAdapter da = new SqlDataAdapter( "Select ID,Name,Branch,City,Active

                                                                                                                from tbl_student", con);





        if (dt.Rows.Count > 0)


            GridView1.DataSource = dt;




    // checking active/inactive and adding image url

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)


        if (e.Row.RowType == DataControlRowType.DataRow)


            ImageButton img = (ImageButton)e.Row.FindControl("img_user");


            if (e.Row.Cells[3].Text == "Active")


                img.ImageUrl = "~/Images/active.png";




                img.ImageUrl = "~/Images/inactive.png";




    //save updated record in database

    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)


        // id of edit row

        string id = GridView1.DataKeys[e.NewSelectedIndex].Value.ToString();

        string status = GridView1.Rows[e.NewSelectedIndex].Cells[3].Text;

        if (status == "Active")






            status = "Active";


        // update record

        SqlCommand cmd = new SqlCommand( "update tbl_student set Active='" +status+"'

                                                                     where ID=" + id, con);