CheckBox in DataGridvview

Posted by Ramu02 under C# on 2/17/2012 | Points: 10 | Views : 1000 | Status : [Member] | Replies : 3
I have a table with the fields like (Sno, Name, Cost, No_Of_Items).
when I want to bind this table into DataGridview with checkbox. In the bottom I have 1 add button and Textbox.....
I will checked some fields in gridview and click on add button, now I want total amount of selected items cost will appear in textbox....

THANK YOU ......




Responses

Posted by: Gow.net on: 2/17/2012 [Member] Starter | Points: 25

Up
0
Down
try this sir
<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>

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;
}
}


gowthaman8870226416

Ramu02, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ramu02 on: 2/17/2012 [Member] Starter | Points: 25

Up
0
Down
Thanks for u r immediate reply ....
But I want to implement this in windows application ....

Ramu02, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pradeepkumar417 on: 2/17/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,

I implemented a code for you. find the attachment. It will help you.
 Download source file

Thanks & Regards,
Software Engineer,
Pradeep Kumar

Ramu02, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response