473,327 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 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 3087
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.