472,805 Members | 1,220 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 software developers and data experts.

CheckBox in DataGrid

Hello all

is it possible to add a checkbox in a DataGrid for
Boolean Data?

Thanks in advance
Nov 17 '05 #1
2 3019
yes!
here's how to add checkbox items to a datagrid in asp.net using c#
e.g. datagrid html code:

.....(html and other bits)
<asp:datagrid id="DgUserPermissions" style="Z-INDEX: 101; LEFT: 44px;
POSITION: absolute; TOP: 152px" runat="server" AutoGenerateColumns="False"
Height="75px" Width="598px" DataKeyField="Project_User_ID">
<AlternatingItemStyle BackColor="LightGray"></AlternatingItemStyle>
<HeaderStyle BackColor="Gray"></HeaderStyle>
<Columns>
<asp:EditCommandColumn ButtonType="PushButton" UpdateText="Update"
CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
.....(some other columns)
<asp:TemplateColumn HeaderText="Disabled">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<asp:CheckBox id="Disabled" runat="server" Checked='<%#
DataBinder.Eval(Container.DataItem, "<NAME OF DATASET COL GOES HERE!!>")
%>'>
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
......(some more columns and html)
which is bound to a dataset (in my case dynamically not at design time)
during the pageload -> if (!page.ispostback) etc.
the dataset column is a bit column in this case

the databinder.eval appears (from what i can tell) to convert the 'bit'
value to something that the asp:checkbox can cope with
if you know what datatype is returned you can put a load of stuff in with
formatting and so on
(note the special syntax "<%#=" this is only executed when the 'databind'
method is called)

and to get the values out?
assuming that you are using the update/edit/cancel button stuff provided, in
your 'update' event
something like this:
(on the codebehind page)
private void DgUserPermissions_Update(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)

put
CheckBox cb = (CheckBox)e.Item.Cells[2].FindControl("Disabled");
Boolean Disabled = cb.Checked;

2 means second column in the grid in this case, the "(checkbox)e...." syntax
is casting the control into a checkbox (which it is, seems silly to me)
you can then access the value
/*
i have had problems where by the edit/cancel buttons were being added but
the hander was missing
(found inside the unexpanded) "Web Form Designer generated code" section
so you have to add in something like (again on the code behind page)
this.DgUserPermissions.CancelCommand += new
System.Web.UI.WebControls.DataGridCommandEventHand ler(this.DgUserPermissions
_Cancel);
this.DgUserPermissions.EditCommand += new
System.Web.UI.WebControls.DataGridCommandEventHand ler(this.DgUserPermissions
_Edit);
this.DgUserPermissions.UpdateCommand += new
System.Web.UI.WebControls.DataGridCommandEventHand ler(this.DgUserPermissions
_Update);
you can put whatever you like in with template columns drop downs, special
formatting and so on
if you don't like c# then make the effort to learn it
i was a vb6 programmer of 5 years and have made the switch (eventually!)

[there are some in depth articles about the datagrid on the 4guysfromrolla
website]

"Sebi" <an*******@discussions.microsoft.com> wrote in message
news:0a****************************@phx.gbl...
Hello all

is it possible to add a checkbox in a DataGrid for
Boolean Data?

Thanks in advance

Nov 17 '05 #2
Thanks

It all worked out!

I'm trying to handle the change of the CheckBox directly.

Don't know yet how to find out which row I'm on but I
hope I'll find out!

I AM C# developer (came from c++) - Thank you!

Sebi
-----Original Message-----
yes!
here's how to add checkbox items to a datagrid in asp.net using c#e.g. datagrid html code:

.....(html and other bits)
<asp:datagrid id="DgUserPermissions" style="Z-INDEX: 101; LEFT: 44px;POSITION: absolute; TOP: 152px" runat="server" AutoGenerateColumns="False"Height="75px" Width="598px" DataKeyField="Project_User_ID"> <AlternatingItemStyle BackColor="LightGray"></AlternatingItemStyle> <HeaderStyle BackColor="Gray"></HeaderStyle>
<Columns>
<asp:EditCommandColumn ButtonType="PushButton" UpdateText="Update"CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>.....(some other columns)
<asp:TemplateColumn HeaderText="Disabled">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<asp:CheckBox id="Disabled" runat="server" Checked='<%#DataBinder.Eval(Container.DataItem, "<NAME OF DATASET COL GOES HERE!!>")%>'>
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
......(some more columns and html)
which is bound to a dataset (in my case dynamically not at design time)during the pageload -> if (!page.ispostback) etc.
the dataset column is a bit column in this case

the databinder.eval appears (from what i can tell) to convert the 'bit'value to something that the asp:checkbox can cope with
if you know what datatype is returned you can put a load of stuff in withformatting and so on
(note the special syntax "<%#=" this is only executed when the 'databind'method is called)

and to get the values out?
assuming that you are using the update/edit/cancel button stuff provided, inyour 'update' event
something like this:
(on the codebehind page)
private void DgUserPermissions_Update(object source,
System.Web.UI.WebControls.DataGridCommandEventArg s e)

put
CheckBox cb = (CheckBox)e.Item.Cells[2].FindControl ("Disabled"); Boolean Disabled = cb.Checked;

2 means second column in the grid in this case, the "(checkbox)e...." syntaxis casting the control into a checkbox (which it is, seems silly to me)you can then access the value
/*
i have had problems where by the edit/cancel buttons were being added butthe hander was missing
(found inside the unexpanded) "Web Form Designer generated code" sectionso you have to add in something like (again on the code behind page) this.DgUserPermissions.CancelCommand += new
System.Web.UI.WebControls.DataGridCommandEventHan dler (this.DgUserPermissions_Cancel);
this.DgUserPermissions.EditCommand += new
System.Web.UI.WebControls.DataGridCommandEventHan dler (this.DgUserPermissions_Edit);
this.DgUserPermissions.UpdateCommand += new
System.Web.UI.WebControls.DataGridCommandEventHan dler (this.DgUserPermissions_Update);
you can put whatever you like in with template columns drop downs, specialformatting and so on
if you don't like c# then make the effort to learn it
i was a vb6 programmer of 5 years and have made the switch (eventually!)
[there are some in depth articles about the datagrid on the 4guysfromrollawebsite]

"Sebi" <an*******@discussions.microsoft.com> wrote in messagenews:0a****************************@phx.gbl...
Hello all

is it possible to add a checkbox in a DataGrid for
Boolean Data?

Thanks in advance

.

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Just D | last post by:
Hi All, Does anybody know how to write the following code? I have a database table with a few columns including: ID , "Task" , ForceRun . I need to show the DataGrid on the ASPX page with...
0
by: mike | last post by:
Hi there: I've read an excellent "how to"-article by Microsoft (no. 306227) - partly cited cited at the end of this email). I have implemented the code related to the part "How to Add a...
1
by: iforsyth | last post by:
Have a paging datagrid with a checkbox control in column(0). ViewState is enabled. I check the checkbox in first row of the grid on a page and then the program hits this event: Private Sub...
1
by: Machi | last post by:
Let say i select rows of records with 4 columns from Database and want to display the data in DataGrid (ASP.NEt Server Control) with one column which must be displayed in CheckBox layout. For the...
0
by: mehul | last post by:
CheckBox template always evaluate to False even if checked in a DataGrid hosted inside a TabStrip in ASP.NET Hi, I am trying to develop an ASP.NET application. I am using TabStrip (which is...
7
by: Lars Netzel | last post by:
If I put a checkbox in a datagrid (ASP.NET) and set the Autopostback to true I can catch OnChange event on checkbox but how do I then catch what DataGridItemIndex is? Can I use some Event in the...
2
by: Adam Knight | last post by:
Hi all, I have a datagrid with a checkbox in one column. The checkbox is set to autopostback and calls a method named UpdateMailSubscribers. The first click on the checkbox cause the page to...
9
by: Rekha | last post by:
The data is filled in datagrid. I want to know how to add a checkbox column in datagrid? In the runtime, checkbox is checked then instead of checkbox value the (caseID )column value is retained....
3
by: Fao, Sean | last post by:
I have a DataGrid that I'm adding CheckBox controls to at runtime (in the code behind) and I'm not sure if I'm doing it correctly. First of all, I noticed that the MyDataGrid.Columns.Add() method...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.