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>

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.