
Introduction
Ever needed to select multiple items from a Data Grid for editing or viewing? Well, using the Item Template, that can be accomplished quite easily.
Setting Up the Template Column
Once you have your Data Grid up and looking how you like, go into the Property Builder and add a Template Column to your Data Grid. Now that you have your Template Column, it's time to add how you want to select the item from the Data Grid. I'll use a Check Box for this example.
Go into your Data Grid and edit the Template Column that you just added. Once the Temple Column is visible, simply drag the Check Box from the Toolbox into the Item Temple row. Once the Check Box is in the Item Template row you can end the Template Editing.
Iterating Through the Data Grid
Now that you've got the Check Box in the Data Grid, it's time to put it to work. The code for iterating through each Check Box to see which ones are check is also very simple:
for (int i = 0; i < DataGridName.Items.Count; i++)
{
DataGridItem dgItem = DataGridName.Items[i];
CheckBox cb = (CheckBox) dgItem.FindControl("CheckBoxName");
if ((cb != null) && (cb.Checked))
}
How It Works
First, you must set up the for loop to go through each item within the Data Grid. Then, you must setup the Data Grid Item, this will hold the individual item found within the Data Grid. You then must set up the control within the Data Grid. I used a Check Box, if you were using a Radio Button, the code would look something like this:
RadioButton rb = (RadioButton) dgItem.FindControl("RadioButtonName");
Once you have obtained the individual control, you can then treat it as it was meant to be, like a control. Then you can just do whatever it is you need to do with that Data Grid Item.
History
- 10 June 2003: First revision.