Home > ASP.Net > Using Checkbox in ASP.NET GridView

Using Checkbox in ASP.NET GridView


Hi all

try this example to Using Checkbox in ASP.NET GridView control

1) Open SQLExpress and Create New Database Called “Test” for example and Add new Table  called “Categories “which has the following colomns :-

 CREATE TABLE [dbo].[Categories](
 [CategoryID] [int] IDENTITY(1,1) NOT NULL,
 [CategoryName] [nvarchar](50) NULL,
 [Approved] [bit] NULL,
 CONSTRAINT [PK_Categories] PRIMARY KEY CLUSTERED
(
 [CategoryID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

and add some data in this Table

2) Now Open VS 2008 and create a new Website and add new Page and from Toolbox add Gridview Control so our page will be like this :-

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
    BackColor="White" BorderColor="#336699" BorderStyle="Solid" BorderWidth="1px" 
    CellPadding="0" CellSpacing="0" DataKeyNames="CategoryID" Font-Size="10"
    Font-Names="Arial" GridLines="Vertical" Width="40%">
            <Columns>           
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:CheckBox ID="chkStatus" runat="server"
                            AutoPostBack="true" OnCheckedChanged="chkStatus_OnCheckedChanged"
                            Checked='<%# Convert.ToBoolean(Eval("Approved")) %>'
                            Text='<%# Eval("Approved").ToString().Equals("True") ? " Approved " : " Not Approved " %>' />
                    </ItemTemplate>                  
                </asp:TemplateField>
                <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" />                  
                <asp:BoundField DataField="CategoryName" HeaderText="CategoryName"  />
            </Columns>
    <HeaderStyle BackColor="#336699" ForeColor="White" Height="20" />
  </asp:GridView>

3) Now we need to write some code to Bind our Gridview with Data from Categories table and also to handle CheckBox Event which will update value of Approved from True to False or from False to True according to Value in table so we will write the following code :-


using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadData();
        }
    }

    private void LoadData()
    {
        string constr = @"Server=.\SQLEXPRESS;Database=Test;Integrated Security=True";
        string query = @"SELECT CategoryID, CategoryName, Approved FROM Categories";
        SqlDataAdapter da = new SqlDataAdapter(query, constr);
        DataTable table = new DataTable();
        da.Fill(table);
        GridView1.DataSource = table;
        GridView1.DataBind();
    }

    public void chkStatus_OnCheckedChanged(object sender, EventArgs e)
    {
        CheckBox chkStatus = (CheckBox)sender;
        GridViewRow row = (GridViewRow)chkStatus.NamingContainer;
        string cid = row.Cells[1].Text;
        bool status = chkStatus.Checked;
        string constr = @"Server=.\SQLEXPRESS;Database=Test;Integrated Security=True";
        string query = "UPDATE Categories SET Approved = @Approved WHERE CategoryID = @CategoryID";
        SqlConnection con = new SqlConnection(constr);
        SqlCommand com = new SqlCommand(query, con);
        com.Parameters.Add("@Approved", SqlDbType.Bit).Value = status;
        com.Parameters.Add("@CategoryID", SqlDbType.Int).Value = cid;
        con.Open();
        com.ExecuteNonQuery();
        con.Close();
        LoadData();
    }
}

now run your website

Hope this helps

Good Luck

Advertisements
Categories: ASP.Net
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: