DataGrid / TemplateColumn / ItemCreated Event Question

Okay, I've been struggling to present my issue in terms people can understand, so here goes my best:

I have a DataGrid that has a list of paragraphs.  In the last column, a templatecolumn, I have a DropDownList for the Order in which the paragraphs are displayed.  The DropDownList is generated from the ItemCreated Event (unless you recommend a better way).

This all works fine.

The issue is getting the existing value of the OrderId from the Database and as the SelectedValue of the DropDownList.

Normally, I would do it similar to this:

But since e.Item.Cells[5].Text is a BoundColumn created in the Event process before this Column is created I cannot grab that value this way.

My question is what would be the best way to accomplish what I'm trying to do?

Thanks, in advance, for your help on the issue! :-)


The theory:
This is part of an admin module of a website.  The theory is that once the order of paragraphs is updated in each column with a value of 1-(MaxNoOfParagraphs) that you would click a button outside the datagrid that would be "Update Paragraph Order".  That would iterate through that collection of rows and update each row with the proper value for the OrderId.

Who is Participating?
dlarlickConnect With a Mentor Commented:

Please switch to the ItemDataBound event.  
The ItemCreated happens prior to the ItemDataBound when the grid does NOT contain data.

ItemDataBound will allow you to obtain data from the cells as indicated in example.


  I use the ItemDataBound event and the FindControl to obtain the dropdownlist within the cell.
  Rough example below, Make sure you have the  TestingDataGrid.ItemDataBound += {etc...} delegate setup.

private void TestingDataGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
   int tempID;

   //Only perform processing for the rows of the grid,
   //**skip the header and footer records from processing**
   if (e.Item.ItemType == ListItemType.Item ||
    e.Item.ItemType == ListItemType.AlternatingItem)
          //Retrieve the status from the datagrid
          tempID= Convert.ToInt32(e.Item.Cells[5].Text);

         DropDownList tempDropDown;
         tempDropDown = (DropDownList) e.Item.Cells[--put your offset here--].FindControl("dlOrder");

rock815Author Commented:
That's pretty much what I'm trying to accomplish using the ItemCreated event.  It has no problem rendering the drop down from there, but I cannot get it to grab the value of another column from the datagrid.  That is my issue entirely.

If I could figure out what Row of the Datagrid the current ItemCreated Event is in, that would theoretically solve everything....

Can you help there?
rock815Author Commented:
Thank you for your help on this, using the right event solved everything!!! :-)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.