I have a grid view and I have a drop down list in several of the cols. A couple have their own data source, and a couple are hard coded. So when I click edit the labels turn to drop down list. BUT, i need to access the drop down lists before the user edits, because I need to set the drop down lists to the current values.

For example if i select option 3, when i click edit and the drop down menu apears I want it to default to option 3. To do this I need to access these drop down lists in my code behind. How do i do this?

I have tried access them in the onediting method but the drop down lists seem to not be created yet.

Thanks for any help!

This is my code of the grid view:

<asp:GridView ID="GridView1" runat="server" Width="100%" 
        AutoGenerateColumns="False" DataSourceID="SqlDataSource1" 
        onselectedindexchanged="GridView1_SelectedIndexChanged" 
        onrowcommand = "GridView1_RowCommand" 
        OnRowUpdating = "GridView1_RowUpdating" 
        OnRowEditing = "GridView1_RowEditing" 
         >
        <Columns>
            
            <asp:TemplateField HeaderText="Reviewed?" SortExpression="rview_ind">
                <ItemTemplate>
                    <asp:Label ID="Label4" runat="server" Text='<%# Bind("rview_ind") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="rview_ind_ddl" runat="server">
                    <asp:ListItem Value="Y">Yes</asp:ListItem>
                    <asp:ListItem Value="N">No</asp:ListItem>
                    </asp:DropDownList>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Valid?" SortExpression="data_valid_ind">
                <ItemTemplate>
                    <asp:Label ID="Label5" runat="server" Text='<%# Bind("data_valid_ind") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="data_valid_ind_ddl" runat="server">
                    <asp:ListItem Value="Y">Valid</asp:ListItem>
                    <asp:ListItem Value="N">InValid</asp:ListItem>
                    </asp:DropDownList>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Invalid Reason" InsertVisible =false  
                SortExpression="invld_data_reasn_id">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("invld_data_reasn_id") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="invld_data_reasn_id_ddl" runat="server" 
                        DataSourceID="SqlDataSource2"  DataTextField="invld_data_reasn_dsc" 
                        DataValueField="invld_data_reasn_id"  >
                    </asp:DropDownList>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Circuit" SortExpression="circt_nm">
                <EditItemTemplate>
                    <asp:Label ID="TextBox2" runat="server" Text='<%# Bind("circt_nm") %>'></asp:Label>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:LinkButton ID="Label2" runat="server" Text='<%# Bind("circt_nm") %>'
                    commandname = "Circuit" CommandArgument = '<%# Bind("dvc_nm") %>'></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Device Alias" SortExpression="dvc_nm">
                <EditItemTemplate>
                    <asp:Label ID="TextBox1" runat="server" Text='<%# Bind("dvc_nm") %>'></asp:Label>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:LinkButton ID="Label3" runat="server" Text='<%# Bind("dvc_nm") %>'
                    commandname = "AssetName" CommandArgument = '<%# Bind("dvc_nm") %>'></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
            
            <asp:BoundField DataField="str_nm" 
                HeaderText="Street" SortExpression="str_nm" />
            <asp:BoundField DataField="city_nm" HeaderText="City" 
                SortExpression="city_nm" />
            <asp:BoundField DataField="st_nm" HeaderText="State" 
                SortExpression="st_nm" />
            <asp:BoundField DataField="map_quad_nm" HeaderText="Quad" 
                SortExpression="map_quad_nm" />
            <asp:BoundField DataField="map_grid_nm" HeaderText="Grid" 
                SortExpression="map_grid_nm" />
            <asp:BoundField DataField="kva_rateg_qty" HeaderText="KVA Rating" 
                SortExpression="kva_rateg_qty" />
            <asp:BoundField DataField="max_kva_rateg_qty" HeaderText="Max KVA" 
                SortExpression="max_kva_rateg_qty" />
            <asp:BoundField DataField="nmplat_loadg_pctg" HeaderText="Load %" 
                SortExpression="nmplat_loadg_pctg" />
            <asp:TemplateField HeaderText="Work Action" InsertVisible="False" 
                SortExpression="work_actn_id">
                <ItemTemplate>
                    <asp:Label ID="Label7" runat="server" Text='<%# Bind("work_actn_id") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="work_actn_id_ddl" runat="server" 
                        DataSourceID="SqlDataSource3" DataTextField="work_actn_dsc" 
                        DataValueField="work_actn_id">
                    </asp:DropDownList>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="comnt_txt" HeaderText="Comment" 
                SortExpression="comnt_txt" />
            <asp:TemplateField HeaderText="WR Needed?" SortExpression="wrqst_need_ind">
                <ItemTemplate>
                    <asp:Label ID="Label6" runat="server" Text='<%# Bind("wrqst_need_ind") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="wrqst_need_ind_ddl" runat="server">
                    <asp:ListItem Value="Y">Yes</asp:ListItem>
                    <asp:ListItem Value="N">No</asp:ListItem>
                    </asp:DropDownList>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="wrqst_cd" HeaderText="WR Number" 
                SortExpression="wrqst_cd" />
            
            
        </Columns>
    </asp:GridView>

Dani AI

Generated

Nice catch by — the DropDownList in an EditItemTemplate only exists after the GridView is rebound into edit mode, so trying to find it in the RowEditing handler will fail. Two lightweight, reliable alternatives avoid extra plumbing and are safer long-term.

The simplest, markup-only approach is to bind the dropdown's selected value directly in the EditItemTemplate so the correct item is chosen when the row is rendered for editing. Example:

<asp:DropDownList ID="invld_data_reasn_id_ddl" runat="server"
  DataSourceID="SqlDataSource2"
  DataTextField="invld_data_reasn_dsc"
  DataValueField="invld_data_reasn_id"
  SelectedValue='<%# Bind("invld_data_reasn_id") %>'>
</asp:DropDownList>

This keeps logic out of code-behind and works when the dropdown is declared with a DataSourceID.

If server-side control is required (for example to populate the list dynamically or to consult other values), the typical flow is: set GridView.EditIndex in RowEditing, rebind the grid, then handle RowDataBound to FindControl on the row that is in edit state and set its SelectedValue (or use GridView.DataKeys to fetch the stored value for that row). Prefer the clearer bitwise test ((row.RowState & DataControlRowState.Edit) != 0) or row.RowState.HasFlag(...) on newer runtimes.

Quick robustness tips: check for nulls before accessing the DropDownList, use Items.FindByValue to avoid exceptions when the desired value is missing, and if the dropdown itself is bound after RowDataBound, consider handling the DropDownList.DataBound event to set SelectedValue there. These patterns keep behavior predictable across postbacks and make the edit UI consistent.

SOLVED:

protected void GridView1_DataBound(object sender, GridViewRowEventArgs e)
    {
        GridViewRow row = e.Row;

        if ((row.RowType == DataControlRowType.DataRow) && ((row.RowState & DataControlRowState.Edit) > 0))
        {
            DropDownList ddl = row.FindControl("invld_data_reasn_id_ddl") as DropDownList;
            Label lb = row.FindControl("Label1") as Label;
            if (ddl != null)
            {
                ddl.SelectedIndex = CodeToDecideWhatToSetDDLToHere;
            }
        }

    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.