Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Iterate through datagrid controls 1

Status
Not open for further replies.

rwbean86

Programmer
Sep 11, 2002
12
0
0
US
I have a datagrid with a template column. The template column contains check boxes. I want to examine all the checkboxes at once when my form posts back to the server. What is the syntax for iterating through a grid's checkbox controls to find those that have been checked?
 
Try this..

Dim count As Integer

For count = 0 To gridName.Rows.Count - 1
If gridName.Rows(count).GetCellValue(gridName.Columns.FromKey("checkbox")) = True Then
'Do something insert/delete/update
End If
Next
 
Thanks for the suggestion, but System.Web.UI.WebControls.DataGrid doesn't have a Rows RowCollection.
 
Thread855-447401
penny1.gif
penny1.gif

The answer to getting answered -- faq855-2992
 
I really appreciated the suggestions. The article on Datagrids is good. However I was able to get the syntax myself via trial and error. Here's how I did it:

ControlCollection CellCtlCol;//control coll for a cell.

CheckBox CheckCtl;//represents the checkbox in the grid's row.

DataGridItemCollection GridCol = Grid.Items;//represents collection of rows/Items

foreach(DataGridItem RowItem in GridCol)
{
CellCtlCol = RowItem.Cells[1].Controls;//get the controls collection for a single cell.

CheckCtl = (CheckBox)CellCtlCol[1];//the 2nd control from the collection is the checkbox

//we have the checkbox control now read its checked property
if(CheckCtl.Checked == true)
{
//create a new xml file for processing
.
.
.
It makes sense. Each cell in the grid has its own control collection so that you can add and remove controls programmatically. This must be how edit columns switch from having a single edit button to having no edit button with a cancel and update button. You access the rows of the grid via the items collection(each of which equates to a row). It's really intuitive if you realize that Items equate to rows, and that each cell has its own collection of controls. One other thing. I would have solved this a lot sooner if I had realized that my checkbox wasn't the only control in the cell's collection. There was a LiteralControl in ahead of the checkbox. When I first saw it I thought I had gone down the wrong path. So be aware of the LiteralControls in the cell's collection.
 
rwbean86, don't overlook the item's .findControl() method.

You pass in the name of a control that you know is there, and it returns to you that control. All you must do is cast it to the proper type, since the method returns an instance of the base object, Control

TextBox myTextBox;
myTextBox = (TextBox)e.Item.FindControl("myTextBox");
penny1.gif
penny1.gif

The answer to getting answered -- faq855-2992
 
Link9,

It would be much easier to use FindControl, but since I'm using the TemplateColumn feature I'm not sure how ASP.NET would ID each checkbox control. Could I specify
ID="MyCheckBox"

and then call

FindControl("MyCheckBox_1");
FindControl("MyCheckBox_2");
.
.
.
FindControl("MyCheckBox_n");
???
 
Nope, if you give it the id, 'myTextBox' in your template column declaration, then as you iterate over the item collection, you can just ask for:

FindControl("myTextBox")

And it drills into that item, and grabs it for you... no special gyrations needed. Just be sure that each item (row) has unique id's for each of it's controls.

Mmmmm.... asp.net. Is there anything it can't do? ;-)
penny1.gif
penny1.gif

The answer to getting answered -- faq855-2992
 
It can't make me supper!
Sorry rwbean86 couldn't help myself. That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Zarcom makes a good point. Supper would be nice.

I have found 1 more caveat to the way I solved my problem. If you do access a cell's controls directly you might find that it has no controls!! This seems to be the case if a column doesn't support editing. In that case the cells themselves have a text property which contains the data.

The moral of the story is: use FindControl like Link9 suggested; then you won't have to worry.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top