DropDownList in DataGrid ItemTemplate

Discussion in 'ASP .Net Datagrid Control' started by Elroyskimms, Mar 20, 2006.

  1. Elroyskimms

    Elroyskimms Guest

    I've been searching for an example of this and I've seen several
    postings pointing to other web pages, but all of those web pages have
    since been removed... or they post pages and pages of sample code and
    my eyes can't take it any more... or you could just call me lazy; what
    ever works for you, but please help! I know how to create a
    DropDownList in the EditItemTemplate and handling the changes in that
    DropDownList, but I need to find out how to use a DropDownList in the

    I have a datagrid with several bound columns, and one template column.
    I want all items to have a DropDownList in that template column when
    the datagrid is first displayed. I need the user to be able to choose
    any of the DropDownLists, change its SelectedItem and I need to handle
    that event. I don't want the user to have to click "Edit", change the
    DropDownList selection, and then click "Update". The user will have
    10-20 items on each page to work on so shaving off those extra
    mouse-clicks will really make a difference.

    Thank you!
    Elroyskimms, Mar 20, 2006
    1. Advertisements

  2. Elroyskimms

    Elroyskimms Guest

    I've spent 2 days trying to figure this one out and I've got it
    working. For posterity and future users with a similar issue, here is
    the solution.

    First, create a DropDownList in the ItemTemplate for the DataGrid.
    Assign its DataSource, DataTextField, and DataValueField properties to
    match the function used to populate the DropDownList. Assign the
    "OnSelectedIndexChanged" property to the name of the procedure you want
    to run when the DropDownList has been changed.

    Bind the datasource to the DataGrid as usual (PageLoad, PostBack,
    whatever makes you feel good). Create a procedure to handle the
    "ItemDataBound" event of the DataGrid. This event will be called for
    each item as it is added to the DataGrid. This procedure should have
    the usual 'sender' and 'e' parameters, of datatype object and
    datagrideventargs (respectively). The 'e' variable represents the
    current item being added to the DataGrid. You can access each of the
    cells or use the FindControl method to search for the DropDownList by
    its ID name. At this point, the DropDownList has been populated from
    the datasource you assigned to it. If you need to change which item in
    the DropDownList is selected or do anything else with the DropDownList
    before it is displayed, here is where you do it. I store the value I
    need to find in a Label control with its Visible property set to false
    (so the user cannot see it). In the ItemDataBound event, I find that
    Label, get its contents and then locate that value in the DropDownList.

    And that's it. The event handler for the DropDownList will capture the
    changes to the DropDownList and the ItemDataBound event will allow you
    to modify each DropDownList in the datagrid.

    Here is the DataGrid ItemTemplate setup:

    <asp:TemplateColumn HeaderText="Status">
    DataSource="<%# qryGetDropDownListData(PublicVariable) %>"

    text='<%# DataBinder.Eval(Container, "DataItem.StatusID") %>'

    <!-- The value must be in the TEXT property in order to access its
    contents in the ItemDataBound event. -->

    Here's the ItemDataBound code:

    Private Sub MyDataGrid_ItemDataBound(ByVal sender As Object, ByVal e As
    System.Web.UI.WebControls.DataGridItemEventArgs) Handles

    If (e.Item.ItemType = ListItemType.Item) Or (e.Item.ItemType =
    ListItemType.AlternatingItem) Then

    Dim MyDDL As DropDownList
    MyDDL = CType(e.Item.Cells(4).FindControl("ddlStatus"),
    MyDDL.SelectedIndex =
    MyDDL.Items.Insert(0, "Status Unassigned...")

    End If

    End Sub

    Hopefully this helps someone else out there, because it took me forever
    to figure out.
    Elroyskimms, Mar 22, 2006
    1. Advertisements

  3. nari reddy

    nari reddy Guest

    Whenever i using this code for DropDownList fire i got one

    "Object reference not set to an instance of an object."
    Description: An unhandled exception occurred during the execution of the
    current web request. Please review the stack trace for more information
    about the error and where it originated in the code.

    Exception Details: System.NullReferenceException: Object reference not
    set to an instance of an object.

    Source Error:

    Line 285: Dim MyDDL As DropDownList
    Line 286: MyDDL = CType(e.Item.Cells(1).FindControl("dd1"),
    Line 287: MyDDL.SelectedIndex =
    ntrol("lh"), Label).Text))
    Line 288: MyDDL.Items.Insert(0, "--Parents list--")
    Line 289:

    Source File: c:\inetpub\wwwroot\Score\WebForm1.aspx.vb Line: 287

    Please give me any solution

    *** Sent via Developersdex http://www.developersdex.com ***
    nari reddy, Apr 8, 2006
  4. Elroyskimms

    Elroyskimms Guest


    I'm sorry that I did not respond sooner. I haven't checked this post

    I can't find anything wrong at first glance. Try breaking down the code
    into smaller pieces to see which component is triggering the exception.
    Use something like this:

    Dim MyDDL As DropDownList
    MyDDL = CType(e.Item.Cells(1).FindControl("dd1"), DropDownList)
    Dim ItemCount as integer
    ItemCount = MyDDL.Items.Count
    Dim SelectedValue as String
    SelectedValue = CType(e.Item.Cells(1).FindControl("lh"), Label).Text
    Dim SelectedItem as ListItem
    SelectedItem = MyDDL.Items.FindByValue(SelectedValue)
    MyDDL.SelectedIndex = MyDDL.Items.IndexOf(SelectedItem)

    If the exception is thrown on the 4th line, that means the DropDownList
    "dd1" was not found in the second cell (Cells are numbered starting at
    0 on the left, not 1). Replace the 2nd line with the following:
    MyDDL = CType(e.Item.FindControl("dd1"), DropDownList)
    This will search all cells in the current list item for the
    DropDownList control "dd1".

    If the exception is thrown on the 6th line, replace it with the
    CType(e.Item.FindControl("lh"), Label).Text
    This will search all cells for the control named "lh". If that line
    still does not work, then there is a problem locating the label "lh".

    If it still is not working, please post the HTML that you are using to
    create the DropDownList, Label, and DataGrid controls.

    Elroyskimms, May 7, 2006
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VB Programmer
    VB Programmer
    Sep 1, 2004
  2. Bennett Haselton
    Joe Fallon
    Sep 24, 2004
  3. Replies:
  4. VB Programmer

    Urgent help with ItemTemplate (datagrid) - databound DropDownList

    VB Programmer, Sep 1, 2004, in forum: ASP .Net Datagrid Control
    VB Programmer
    Sep 1, 2004
  5. fnuser
    Jan 26, 2013