By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,682 Members | 1,999 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,682 IT Pros & Developers. It's quick & easy.

Adding a CheckBox to a DataGrid at Runtime

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
Fao, Sean wrote:
What is the *proper* way to add a CheckBox to a DataGrid in code?


Anybody?
Jan 27 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.