Click here to Skip to main content
15,881,757 members
Articles / Web Development / ASP.NET
Article

Item Template with Data Grid

Rate me:
Please Sign up or sign in to vote.
1.60/5 (21 votes)
9 Jun 20031 min read 144.5K   34   9
Ever needed to select multiple items from a Data Grid for editing or viewing? Using the Item Template allows this to be accomplished quite easily.

Sample Image - ITDataGri.jpg

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:

C#
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))
           //[Your Code Here]
}

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:

C#
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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
"..Been walking the earth since child-birth. I never went to school, and when I did I ate my homework. Never played it cool, and if I did I was a poser. You can't blame a nerd for trying to get closer. Now I'm the poster boy for geeks and freaks..."

My profile according to Google

I like tha moon

All Your Base Are Belong To Us

Think different, think beige.

Domopers

Disclaimer:

Any and all things I say, I in no way think myself better then anyone else. I have my fair share (well, a good deal) of problems. I just like to gripe whenever given the chance

Comments and Discussions

 
GeneralMy vote of 1 Pin
Member 998470913-Apr-13 0:14
Member 998470913-Apr-13 0:14 
QuestionI am not getting the DataGridItem class Pin
salamkudru11-Apr-13 20:12
salamkudru11-Apr-13 20:12 
GeneralDrop Down List in Datagrid Pin
Hazem Salem7-Apr-04 9:22
Hazem Salem7-Apr-04 9:22 
GeneralRe: Drop Down List in Datagrid Pin
Anonymous28-May-04 0:32
Anonymous28-May-04 0:32 
GeneralRe: Drop Down List in Datagrid Pin
Anonymous15-Oct-04 2:04
Anonymous15-Oct-04 2:04 
GeneralRe: Drop Down List in Datagrid Pin
Anonymous28-May-04 0:32
Anonymous28-May-04 0:32 
GeneralBound data for DropDownList Pin
toanvd24-Sep-03 23:24
toanvd24-Sep-03 23:24 
Generalupdate&amp;add&amp;delete Pin
toanvd24-Sep-03 23:21
toanvd24-Sep-03 23:21 
Questionhow to get the check box control Pin
skmrizwan22-Jul-03 19:30
skmrizwan22-Jul-03 19:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.