DataGrid / TemplateColumn / ItemCreated Event Question

Posted on 2006-05-02
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
    LVL 2

    Expert Comment


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

    LVL 1

    Author Comment

    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?
    LVL 2

    Accepted Solution


    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.

    LVL 1

    Author Comment

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

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Suggested Solutions

    In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
    In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    731 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now