Multimobile Development: Building Applications for any Smartphone
Checkbox in Datagrid
Messages   Related Types
This message was discovered on microsoft.public.dotnet.distributed_apps.


Anderson
GOOD ANSWER
Hi all,

I've been trying to have a simple checkbox in my datagrid
using C# and it's being a pain to get it to work.
Here is the cenario.

I have a DataSet populated with a table "Employees" let's
say. This table has fields such id, name, email.
All I want to do is to display an extra column in my
datagrid that will display a checkbox for each row.
Note, there is no data associated with this checkbox. I
just wanted the user to select some of the Employees from
the datagrid by checking the checkbox. Then, when they
click the button "Print", I will loop through the
datagrid, get the selected ones and print.

Anyone can give me some instructions or example of how to
do that?
I've been looking some people examples that do something
similar. But they don't really show how to add an extra
column to the datagrid with a checkbox for each record of
your already populated dataset.

Please, any help will be appreciated.
Thanks to all.

Anderson
Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
    
Tim Cleland
GOOD ANSWER
In your Web Form (aspx page) add something like the following to your
datagrid

<asp:datagrid id="dgEmployees" runat="server"...
....
<asp:TemplateColumn HeaderText="Select">
<ItemTemplate>
<asp:CheckBox id="chkEmployees" runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
....
</asp:datagrid>

in your code behind on the "print" handler add the following:

DataGridItem dgItem = null;
int iEmployeeId = 0;
CheckBox chkBox = null;

for (int i=0; i < dgEmployees.Items.Count; i++)
{
dgItem = dgEmployees.Items[i];
chkBox = (CheckBox)dgItem.FindControl("chkEmployees");
if ((chkBox != null) && chkBox.Checked)
{
// Assuming you ID column is the first cell
iEmployeeId = int.Parse(dgItem.Cells[0].Text);
//Now run your print code here for this employee id

//Response.Write(String.Format("{0}<br>",iEmployeeId.ToString()));
}
}

Tim C
timc[NOSpAmmY]@techie.com[REMOVE]

On Tue, 9 Apr 2002 04:13:27 -0700, "Anderson" <Click here to reveal e-mail address>
wrote:

[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
    
Tim Cleland
GOOD ANSWER
In your Web Form (aspx page) add something like the following to your
datagrid

<asp:datagrid id="dgEmployees" runat="server"...
....
<asp:TemplateColumn HeaderText="Select">
<ItemTemplate>
<asp:CheckBox id="chkEmployees" runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
....
</asp:datagrid>

in your code behind on the "print" handler add the following:

DataGridItem dgItem = null;
int iEmployeeId = 0;
CheckBox chkBox = null;

for (int i=0; i < dgEmployees.Items.Count; i++)
{
dgItem = dgEmployees.Items[i];
chkBox = (CheckBox)dgItem.FindControl("chkEmployees");
if ((chkBox != null) && chkBox.Checked)
{
// Assuming you ID column is the first cell
iEmployeeId = int.Parse(dgItem.Cells[0].Text);
//Now run your print code here for this employee id

//Response.Write(String.Format("{0}<br>",iEmployeeId.ToString()));
}
}

Tim Cleland
timc[NOSpAmmY]@techie.com[REMOVE]

On Tue, 9 Apr 2002 04:13:27 -0700, "Anderson" <Click here to reveal e-mail address>
wrote:

[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
    
suhas shinde
GOOD ANSWER
(Type your message here)

--------------------------------
From: suhas shinde
Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
    
viswa babu
GOOD ANSWER
(Type your message here)

--------------------------------
From: viswa babu
Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
 
System.Data.DataSet
System.String
System.Web.UI.WebControls.CheckBox
System.Web.UI.WebControls.DataGridItem
System.Web.UI.WebControls.TemplateColumn
System.Windows.Forms.CheckBox




Ad
BootFX
Reliable and powerful .NET application framework.
iOS, Android and Windows Phone Development Training and Consultancy
Hosted by RackSRV Communications
 
Multimobile Development: Building Applications for any Smartphone
Copyright © AMX Software Ltd 2008-2010. Portions copyright © Matthew Baxter-Reynolds 2001-2010. All rights reserved.
Contact Us - Terms of Use - Privacy Policy - 4.0.30129.1734