Setting SelectedValue in DataGrid's DropDownLists

Discussion in 'ASP .Net Datagrid Control' started by AlBruAn, Apr 19, 2006.

  1. AlBruAn

    AlBruAn Guest

    I have an application I'm working on in which I'm wanting to default to
    displaying the previously selected value from a dropdownlist whenever the
    user opts to edit a record in the datagrid control. I'm able to grab the
    previously selected value from the text box, but I can't set the
    SelectedValue in the corresponding dropdownlist equal to it. Code is as

    458 Control ctl;
    459 DataGridItem dgRow = e.Item;
    461 ctl = e.Item.FindControl("lblFlooring");
    462 currFlooring = ((Label)ctl).Text;
    464 DataGrid1.EditItemIndex = e.Item.ItemIndex;
    465 BindGrid();
    467 ((DropDownList)dgRow.FindControl("ddlFlooring")).SelectedValue =

    When I attempt to execute line 467 in this code, I get the standard ""Object
    reference not set to an instance of an object." message. BTW, lblFlooring is
    the label containing the flooring material prior to entering the Edit mode
    and ddlFlooring is the dropdownlist containing flooring materials once a row
    has been placed in Edit mode. Anyway, is there another way of setting the
    SelectedValue as part of the datagrid's EditCommand function? Any help would
    be greatly appreciated.
    AlBruAn, Apr 19, 2006
    1. Advertisements

  2. AlBruAn

    AlBruAn Guest

    Never mind. I needed to add an OnPreRender reference to a function in the
    HTML defining the dropdownlist In said function, I grab the ListControl for
    the DropDownList and set its SelectedValue equal to what'd previously been
    displayed in that cell.
    AlBruAn, Apr 20, 2006
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.