Click here to Skip to main content
Licence 
First Posted 9 Jun 2003
Views 123,914
Bookmarked 34 times

Item Template with Data Grid

By | 9 Jun 2003 | Article
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:

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:

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

About the Author

Zachery

Web Developer

United States United States

Member

"..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


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralDrop Down List in Datagrid PinmemberHazem Salem9:22 7 Apr '04  
GeneralRe: Drop Down List in Datagrid PinsussAnonymous0:32 28 May '04  
GeneralRe: Drop Down List in Datagrid PinsussAnonymous2:04 15 Oct '04  
GeneralRe: Drop Down List in Datagrid PinsussAnonymous0:32 28 May '04  
GeneralBound data for DropDownList Pinmembertoanvd23:24 24 Sep '03  
Generalupdate&add&delete Pinmembertoanvd23:21 24 Sep '03  
Questionhow to get the check box control Pinmemberskmrizwan19:30 22 Jul '03  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 10 Jun 2003
Article Copyright 2003 by Zachery
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid