473,804 Members | 3,532 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3160
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="DgUserPermi ssions" style="Z-INDEX: 101; LEFT: 44px;
POSITION: absolute; TOP: 152px" runat="server" AutoGenerateCol umns="False"
Height="75px" Width="598px" DataKeyField="P roject_User_ID" >
<AlternatingIte mStyle BackColor="Ligh tGray"></AlternatingItem Style>
<HeaderStyle BackColor="Gray "></HeaderStyle>
<Columns>
<asp:EditComman dColumn ButtonType="Pus hButton" UpdateText="Upd ate"
CancelText="Can cel" EditText="Edit" ></asp:EditCommand Column>
.....(some other columns)
<asp:TemplateCo lumn HeaderText="Dis abled">
<ItemStyle HorizontalAlign ="Center"></ItemStyle>
<ItemTemplate >
<asp:CheckBox id="Disabled" runat="server" Checked='<%#
DataBinder.Eval (Container.Data Item, "<NAME OF DATASET COL GOES HERE!!>")
%>'>
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateCol umn>
......(some more columns and html)
which is bound to a dataset (in my case dynamically not at design time)
during the pageload -> if (!page.ispostba ck) 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 DgUserPermissio ns_Update(objec t source,
System.Web.UI.W ebControls.Data GridCommandEven tArgs e)

put
CheckBox cb = (CheckBox)e.Ite m.Cells[2].FindControl("D isabled");
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.DgUserPerm issions.CancelC ommand += new
System.Web.UI.W ebControls.Data GridCommandEven tHandler(this.D gUserPermission s
_Cancel);
this.DgUserPerm issions.EditCom mand += new
System.Web.UI.W ebControls.Data GridCommandEven tHandler(this.D gUserPermission s
_Edit);
this.DgUserPerm issions.UpdateC ommand += new
System.Web.UI.W ebControls.Data GridCommandEven tHandler(this.D gUserPermission s
_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*******@disc ussions.microso ft.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="DgUserPermi ssions" style="Z-INDEX: 101; LEFT: 44px;POSITION: absolute; TOP: 152px" runat="server" AutoGenerateCol umns="False"Height="75px " Width="598px" DataKeyField="P roject_User_ID" > <AlternatingIte mStyle BackColor="Ligh tGray"></AlternatingItem Style> <HeaderStyle BackColor="Gray "></HeaderStyle>
<Columns>
<asp:EditComman dColumn ButtonType="Pus hButton" UpdateText="Upd ate"CancelText="Ca ncel" EditText="Edit" ></asp:EditCommand Column>.....(some other columns)
<asp:TemplateC olumn HeaderText="Dis abled">
<ItemStyle HorizontalAlign ="Center"></ItemStyle>
<ItemTemplate >
<asp:CheckBox id="Disabled" runat="server" Checked='<%#DataBinder.Eva l(Container.Dat aItem, "<NAME OF DATASET COL GOES HERE!!>")%>'>
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateCol umn>
......(some more columns and html)
which is bound to a dataset (in my case dynamically not at design time)during the pageload -> if (!page.ispostba ck) 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 DgUserPermissio ns_Update(objec t source,
System.Web.UI. WebControls.Dat aGridCommandEve ntArgs e)

put
CheckBox cb = (CheckBox)e.Ite m.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.DgUserPerm issions.CancelC ommand += new
System.Web.UI. WebControls.Dat aGridCommandEve ntHandler (this.DgUserPer missions_Cancel);
this.DgUserPerm issions.EditCom mand += new
System.Web.UI. WebControls.Dat aGridCommandEve ntHandler (this.DgUserPer missions_Edit);
this.DgUserPerm issions.UpdateC ommand += new
System.Web.UI. WebControls.Dat aGridCommandEve ntHandler (this.DgUserPer missions_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*******@disc ussions.microso ft.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
2162
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 these selected fields and allow user to click one or more CheckBox controls to activate this bit on the database table for a required ID. I have already added a template column to the DataGrid and I can get the
0
2408
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 CheckBox Programmatically" in my code. I have changed it to use a OLDDB.DBReader to populate the datagrid and for the databinding. It works perfectly - also when using the edit-mode in the datagrid.
1
4162
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 dgRegGrid_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles dgRegGrid.PageIndexChanged I then do a loop to check the checkbox state.
1
663
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 column (which contains checkbox), when i click the CheckBox in the Header of DataGrid, all the rows (for column which contains Checkboxes) must be checked. When i uncheck the checkbox in Header of DataGrid, all the rows of that column must be unchecked,...
0
1860
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 part of IE WebControls). Inside a tab I have a datagrid defined as follows:
7
2281
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 Datagrid that will fire off when the Checkbox is changed? regards /Lars
2
3053
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 post but doesn't seem to haven't any affect. It doesn't appear to call the UpdateMailSubscribers method.
9
1597
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. I know there is a lot of article on datagrid with checkbox but nothing helped me . Can anyone help me how to proceed ? Thanks
3
3154
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 expects a DataGridColumn so I instantiated an object of type TemplateColumn that I had hoped I could add a CheckBox to. I soon discovered that the ItemTemplate property of the TemplateColumn class returned an object that had implemented the...
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9582
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10335
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10323
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10082
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9157
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7621
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5652
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.