473,399 Members | 2,478 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,399 software developers and data experts.

Adding a CheckBox to a DataGrid at Runtime

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 ITemplate interface and the CheckBox class did
not implement it out of the box.

From there, it meant that I would have to create a new class, which
inherited from CheckBox and implemented the ITemplate interface. I did
that and everything works (I have a CheckBox on my DataGrid), but it
seemed like the long way around and I figured there must be an easier
way. For example, how does ASP.NET add a CheckBox to a DataGrid if I
had just added it to my HTML? When I add a CheckBox in HTML, I never
have to implement the ITemplate interface --it appears that it happens
automatically. So what goes on behind the scenes that makes this possible?

As I said, I successfully added a CheckBox to my DataGrid; however, it
meant creating a new class, which I'd rather not use if it's possible.
Also, I'm having a strange issue come up that I think might be related
to my custom class.

What is the *proper* way to add a CheckBox to a DataGrid in code?

Thank you in advance,

--
Sean
Jan 26 '06 #1
3 3122
Options:

1. Switch to repeater. Allows you to play with the HTML table as you wish.
More flexible, but a bit harder to maintain.

2. Add dynamically using Add(). If you are simply adding the same way for
each row, this can be easy to maintain. If you persist the state or need
different states based on data, this is a pain.

3. Use the DataGrid events. You can catch when a row is being added and
dynamically add the cell at this time. The potential downside is creating a
very tightly coupled/dependent UI.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
"Fao, Sean" wrote:
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 ITemplate interface and the CheckBox class did
not implement it out of the box.

From there, it meant that I would have to create a new class, which
inherited from CheckBox and implemented the ITemplate interface. I did
that and everything works (I have a CheckBox on my DataGrid), but it
seemed like the long way around and I figured there must be an easier
way. For example, how does ASP.NET add a CheckBox to a DataGrid if I
had just added it to my HTML? When I add a CheckBox in HTML, I never
have to implement the ITemplate interface --it appears that it happens
automatically. So what goes on behind the scenes that makes this possible?

As I said, I successfully added a CheckBox to my DataGrid; however, it
meant creating a new class, which I'd rather not use if it's possible.
Also, I'm having a strange issue come up that I think might be related
to my custom class.

What is the *proper* way to add a CheckBox to a DataGrid in code?

Thank you in advance,

--
Sean

Jan 26 '06 #2
Hi Gregory,

First of all, thank you very much for your response.

Cowboy (Gregory A. Beamer) - MVP wrote:
Options:

1. Switch to repeater. Allows you to play with the HTML table as you wish.
More flexible, but a bit harder to maintain.
I actually created two template pages so that I could later decide which
version I prefer. I need the ability to "Select" a record, so a
Repeater was out of the question. Fortunately, a DataList *did* provide
the functionality I need (minus paging and the ability to sort). I
successfully implemented paging support in a custom DataList control by
creating a SQL Stored Procedure with paging support. I have not yet
implemented sorting in my custom DataList control and probably will not
do so (yet). The other template (the one with the DataGrid), however,
does [obviously] have the ability to sort.

At any rate, adding CheckBox's in a DataList was quite a bit easier than
implementing it in a DataGrid. However, it's important to note that the
DataGrid control that I'm using is actually another custom control,
which inherits from the DataGrid class and implements a few nice
features. I'd like to keep these additional features because it would
make this page consistent with other pages that also use the custom
DataGrid control.
2. Add dynamically using Add(). If you are simply adding the same way for
each row, this can be easy to maintain. If you persist the state or need
different states based on data, this is a pain.
Unless you're referring to something else, this is what I'm already
doing (as well as the way that I prefer). The problem is that it
required that I create a custom CheckBox class, which I thought was the
long way around.
3. Use the DataGrid events. You can catch when a row is being added and
dynamically add the cell at this time. The potential downside is creating a
very tightly coupled/dependent UI.


I'd prefer to avoid this approach because of the potential downside you
listed above.
Thank you again for your response,

--
Sean
Jan 26 '06 #3
Fao, Sean wrote:
What is the *proper* way to add a CheckBox to a DataGrid in code?


Anybody?
Jan 27 '06 #4

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

Similar topics

1
by: The_Rave | last post by:
Hi everyone, I'm trying to add my own template columns to the property builder of ..NET. E.g. a checkbox column, or an image column. But I can't find the sources of the wizard, or a way to add...
3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
1
by: DJ Dev | last post by:
Hi, I am stuck at a problem for 3 days now. I create runtime datagrids depending on the user selections and they may vary from 2-10 depending on user selection at runtime. The datagrids are...
19
by: Sjaakie Helderhorst | last post by:
Hello, I need to add a Javascript event (onClick) to a servercontrol (asp:checkbox). Checkbox is named 'cbDoMail'. Assumed that 'cbDoMail.Attributes.Add("onClick", "doJavascriptThing()") would do...
2
by: Bob Hollness | last post by:
Hi group. I am a newbie to ASP.NET as you will see from some of the questions I may ask! I have a datagrid which I have populated from a database. It works great! I have added a column, via...
1
by: Dexter | last post by:
Hello all, I have a datagrid with a template column with checkbox, and at this template column, at header section, i have a checkbox with autopostback property as true, and when i check this...
1
by: sianan | last post by:
I tried to use the following example, to add a checkbox column to a DataGrid in an ASP.NET application: http://www.codeproject.com/aspnet/datagridcheckbox.asp For some reason, I simply CAN'T get...
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....
1
by: Abhishek | last post by:
I am creating a Datagrid at runtime and I have to create all the columns of grid at runtime... I am facing problem in the item template column. Here I want to place more than one control... For...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...
0
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...

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.