connectseekers.com

Wednesday, 17 August 2016

CheckBox Control Checked Uncheked By DataTable Value in Itemtemplate in Gridview in Asp.net Uisng C#

8/17/2016 - By Pranav Singh 0

This article will show you how to use checkbox control in asp.net gridview to update database c#. It will show use of checkbox control checked uncheked by datatable value in itemtemplate in gridview in asp.net uisng c#.


So for this article first we will create a new asp.net application and add the gridview control and add the below code into the page.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm11.aspx.cs" Inherits="WebApplication7.WebForm11" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>CheckBox Control Checked Uncheked By DataTable Value in Itemtemplate in Gridview in Asp.net Uisng C#</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
                <Columns>
                    <asp:BoundField DataField="Name" HeaderText="Name" />
                    <asp:TemplateField HeaderText="Status">
                        <ItemTemplate>
                            <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%#Convert.ToBoolean(Eval("Status"))  %>' />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </div>
    </form>
</body>
</html>

In above code I have bind the checkbox in the gridview itemtemplate. Convert.ToBoolean will help to make the check and uncheck to checkbox on the bases of the passes databased value.
Now check the code.

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

namespace WebApplication7
{
    public partial class WebForm11 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt = GetFileDetail();
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
        protected bool GetStatus(string str)
        {
            if (str == "1")
                return true;
            else
                return false;
        }
        public DataTable GetFileDetail()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Name");
            dt.Columns.Add("Status");

            DataRow dataRow1 = dt.NewRow();
            dataRow1["Name"] = "Name 1";
            dataRow1["Status"] = true;
            dt.Rows.Add(dataRow1);

            DataRow dataRow2 = dt.NewRow();
            dataRow2["Name"] = "Name 2";
            dataRow2["Status"] = false;
            dt.Rows.Add(dataRow2);

            DataRow dataRow3 = dt.NewRow();
            dataRow3["Name"] = "Name 3";
            dataRow3["Status"] = true;
            dt.Rows.Add(dataRow3);


            return dt;
        }
    }
}

In above code I have created a datatable, which will prepare the data to bind the data to the gridview as a datasource. Now on page load I have bind the gridview. In above code we must assign Boolean value to the field which we are going to bind it with the checkbox control.


Now we have done run the application to check the output.


About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com
Email Id : contact@aspdotnet-pools.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014
back to top