6 Replies - 154846 Views - Last Post: 28 November 2011 - 04:11 AM Rate Topic: -----

#1 new2programs  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 29
  • Joined: 10-April 07

checkbox in gridview

Posted 11 May 2007 - 10:32 AM

hi,
i'm new to asp.net and i want some help.I am trying to create an asp.net page with c#.it has a gridview which will be populated with item from the DB when the page is loaded.
I want to add a checkbox column and after teh user checks on one item, on the click of a button he should be able to purchse it.

That is, after the button is clicked i should be able to load that selected item into my DB table. can anyone please help.

I tried to add the checkbox field but i dont know how to go ahead.
Please help!

Is This A Good Question/Topic? 1

#10 JellyBean  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 60
  • Joined: 25-April 07

Re: checkbox in gridview

Posted 11 May 2007 - 12:43 PM

You need to define a checkbox column as a TemplateField.

Below is an example showing how to retrieve the key of each checked item.

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

<!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>CheckBox Example</title>
</head>
<body>
	<form id="form1" runat="server">
	<div>
		<asp:GridView ID="ProductGridView" runat="server" AutoGenerateColumns="False" DataKeyNames="ItemId">
			<Columns>
				<asp:BoundField DataField="ItemDescription" DataFormatString="{0}" HeaderText="Item" />
				<asp:BoundField DataField="ItemPrice" DataFormatString="{0:C}" HeaderText="Price" />
				<asp:TemplateField HeaderText="Purchase">
					<ItemTemplate>
						<asp:CheckBox ID="CheckBoxPurchase" runat="server" Enabled="true" />
					</ItemTemplate>
				</asp:TemplateField>
			</Columns>
		</asp:GridView>
		<asp:Button ID="Purchase" runat="server" Text="Purchase Checked Items" />
	</div>
	</form>
</body>
</html>


CheckBoxExample.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class CheckBoxExample : System.Web.UI.Page
{
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!IsPostBack)
		{
			// Set up the datagrid
			DataTable productData = CreateData();
			ProductGridView.DataSource = productData;
			ProductGridView.DataBind();
		}
		else
		{
			// Are there checked boxes?
			List<int> purchaseProductList = new List<int>();
			for (int i = 0; i < ProductGridView.Rows.Count; i++)
			{
				int productId = (int)ProductGridView.DataKeys[i][0];
				CheckBox cb = (CheckBox)ProductGridView.Rows[i].FindControl("CheckBoxPurchase");
				if (cb.Checked)
				{
					purchaseProductList.Add(productId);
				}
			}
			// Do something with purchaseProductList
		}
	}

	private DataTable CreateData()
	{
		DataTable t = new DataTable();
		t.Columns.Add(new DataColumn("ItemId", typeof(int)));
		t.Columns.Add(new DataColumn("ItemDescription", typeof(string)));
		t.Columns.Add(new DataColumn("ItemPrice", typeof(decimal)));
		DataRow r = t.NewRow();
		r["ItemId"] = 1;
		r["ItemDescription"] = "Diamond Bling Grill";
		r["ItemPrice"] = 19.99;
		t.Rows.Add(r);
		r = t.NewRow();
		r["ItemId"] = 2;
		r["ItemDescription"] = "Silver Toaster";
		r["ItemPrice"] = 9.99;
		t.Rows.Add(r);
		return t;
	}
}


Hope this helps. ;)
Was This Post Helpful? 1

#14 foramit1581  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 1
  • Joined: 24-August 07

Re: checkbox in gridview

Posted 24 August 2007 - 10:38 AM

HI,

Thanks for this article....but i m getting an error while selecting DATAKEYS...It saying that "Index out of range......should be non negative..." what should I do???

Amit

View PostJellyBean, on 11 May, 2007 - 12:43 PM, said:

You need to define a checkbox column as a TemplateField.

Below is an example showing how to retrieve the key of each checked item.

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

<!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>CheckBox Example</title>
</head>
<body>
	<form id="form1" runat="server">
	<div>
		<asp:GridView ID="ProductGridView" runat="server" AutoGenerateColumns="False" DataKeyNames="ItemId">
			<Columns>
				<asp:BoundField DataField="ItemDescription" DataFormatString="{0}" HeaderText="Item" />
				<asp:BoundField DataField="ItemPrice" DataFormatString="{0:C}" HeaderText="Price" />
				<asp:TemplateField HeaderText="Purchase">
					<ItemTemplate>
						<asp:CheckBox ID="CheckBoxPurchase" runat="server" Enabled="true" />
					</ItemTemplate>
				</asp:TemplateField>
			</Columns>
		</asp:GridView>
		<asp:Button ID="Purchase" runat="server" Text="Purchase Checked Items" />
	</div>
	</form>
</body>
</html>


CheckBoxExample.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class CheckBoxExample : System.Web.UI.Page
{
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!IsPostBack)
		{
			// Set up the datagrid
			DataTable productData = CreateData();
			ProductGridView.DataSource = productData;
			ProductGridView.DataBind();
		}
		else
		{
			// Are there checked boxes?
			List<int> purchaseProductList = new List<int>();
			for (int i = 0; i < ProductGridView.Rows.Count; i++)
			{
				int productId = (int)ProductGridView.DataKeys[i][0];
				CheckBox cb = (CheckBox)ProductGridView.Rows[i].FindControl("CheckBoxPurchase");
				if (cb.Checked)
				{
					purchaseProductList.Add(productId);
				}
			}
			// Do something with purchaseProductList
		}
	}

	private DataTable CreateData()
	{
		DataTable t = new DataTable();
		t.Columns.Add(new DataColumn("ItemId", typeof(int)));
		t.Columns.Add(new DataColumn("ItemDescription", typeof(string)));
		t.Columns.Add(new DataColumn("ItemPrice", typeof(decimal)));
		DataRow r = t.NewRow();
		r["ItemId"] = 1;
		r["ItemDescription"] = "Diamond Bling Grill";
		r["ItemPrice"] = 19.99;
		t.Rows.Add(r);
		r = t.NewRow();
		r["ItemId"] = 2;
		r["ItemDescription"] = "Silver Toaster";
		r["ItemPrice"] = 9.99;
		t.Rows.Add(r);
		return t;
	}
}


Hope this helps. ;)

Was This Post Helpful? 1

#15 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: checkbox in gridview

Posted 24 August 2007 - 11:29 AM

JellyBean why are you giving him a complete solution when he hasn't put forth any effort, hasn't shown any code to show he's tried or anything? That is not what this community is about, we're not here to do peoples work for them.
Was This Post Helpful? 2

#16 Guest_DKV*


Reputation:

Re: checkbox in gridview

Posted 22 June 2010 - 02:33 PM

This looks like a good example that I can use, however I don't understand the DataSource part of it. how did you connect it to a table in a database? Where is the connection string, open connection execute query etc? How does this line of code works: ProductGridView.DataSource = productData;
I would like to connect it to Colors table in my database to select colors.

Thanks.
Was This Post Helpful? 0

#17 Frinavale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 203
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: checkbox in gridview

Posted 22 June 2010 - 08:58 PM

To connect to a database you need two components.
(I'm going to assume that you're using a MS SQL database for this answer)

You need a SqlConnection and a SqlCommand.

You also need something that will contain the data that is retrieved from the database.
You can use something like a SqlDataReader or a DataAdapter.

-Frinny
Was This Post Helpful? 0
  • +
  • -

#18 gebhushan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 28-November 11

Re: checkbox in gridview

Posted 28 November 2011 - 04:11 AM

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
else
{
if (CheckBox1.Checked == true)
{
TemplateField col = new TemplateField();
CheckBox c = new CheckBox();
col.Initialize(true, c);
col.HeaderText = "Student";
GridView1.Columns.Add(col);
CheckBoxField cb = new CheckBoxField();
}
}
}
I'm trying to build a survey form.
In which the minimum limit is one person and the maximum no. of people to survey is 10.
I'm able to create a gridview with check boxex in one column but. when i want to increase a column based on a button click or combo selection using the above code(which was based on a check box outside gridview) i am generate a column after selecting it twice but i'm not able to see the check boxes.
kindly help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1