DataGrid / TemplateColumn / ItemCreated Event Question

Posted on 2006-05-02
Medium Priority
Last Modified: 2008-03-04
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.

Question by:rock815
  • 2
  • 2

Expert Comment

ID: 16591775

  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");


Author Comment

ID: 16598910
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?

Accepted Solution

dlarlick earned 1000 total points
ID: 16601149

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.


Author Comment

ID: 16605155
Thank you for your help on this, using the right event solved everything!!! :-)

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Integration Management Part 2
Suggested Courses
Course of the Month14 days, 18 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question