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#.
Some of my
previous articles are as follows: Show
Images in Gridview from Folder in Asp.net, Asp.net
Gridview Row Font Colour Change Based on Condition or Data Using C#.Net, Asp.net
Gridview Row Italic Based on Condition or Data Using C#.Net, Asp.net
Gridview Row Bold Based on Condition or Data Using C#.Net, Asp.net
Gridview Row Color Change Based on Condition or Data Using C#.Net, Bind
HyperLink Control To GridView In Asp.net Using C#, Enable
Or Disable All GridView Button On Check Of CheckBox Using jQuery, Bind
HyperLink Control With URL Parameter To GridView In Asp.net Using 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.
0 comments:
Please let me know your view