|
Displaying a CheckBox or RadioButton in Each Row of a DataGrid
By:
Scott Mitchell
|
Created: 2003-05-09
|
Last Updated: 2003-05-15
|
Printer-Friendly Version
Imagine that you had a DataGrid that listed a series of products, and you wanted the user to select a single product from the list of products. One way to accomplish this would be to have a column in the DataGrid that displays nothing but a radio button. Then, a user would indicate what product he was interested in by clicking on the appropriate radio button and, perhaps, clicking a "Continue" button outside of the DataGrid. On a related note, you might want to allow the user to choose zero, one, two, or more of the products, and continue to the next page. For this scenario, you'd want a column of checkboxes. (To see what I mean by a column of radio buttons or checkboxes, check out this screenshot.) When first presented with this problem, you may initially reason that you can just plunker down a TemplateColumn and add a RadioButton or CheckBox Web control in the ItemTemplate. While this approach works for the CheckBox demands, it has some shortcomings with the RadioButton. These problems are discussed in detail in an earlier article of mine, An Extensive Examination of the DataGrid Web Control.
As I discussed in my earlier article, a rather simple and straightforward solution to this problem is to use
Andy Smith's fine RowSelectorColumn control. The RowSelectorColumn, which is
available for free from Andy's Web site, is a free
DataGrid column control. DataGrid column controls, as discussed in Chapter 12 of ASP.NET Data Web Controls, are custom-made
compiled classes that can be used in the DataGrid's
The benefit of creating custom DataGrid column controls is that they can be used just like the standard DataGrid columns -
that is, all of the code and complexity they contain is neatly tucked away. All that you need to do is add a single
line to your DataGrid's
The line
Andy's control also allows for a column of checkboxes. Furthermore, it can even display a "Select All" checkbox in the header of the column of checkboxes, such that when the "Select All" checkbox is clicked, all of the checkboxes are automatically either selected or unselected (depending on whether you are selecting or unselecting the "Select All" checkbox). For more information on the RowSelectorColumn, be sure to check out Andy's Web site, MetaBuilders.com. In addition to the RowSelectorColumn, he has a number of other free DataGrid column controls available for free.
|
Home | FAQs | Articles | About | My Blog | Buy the Book!
Copyright 2006, Scott Mitchell. All Rights Reserved.