3

How do i set selectedvalue of dropdownlist if dropdownlist is within Gridview and dropdownlist is populated by objectdatasource when EDIT button is clicked?

What happens to my app is dropdownlist gets populated but the first item always shows up as selectedvalue.

Fishcake
  • 10,496
  • 7
  • 44
  • 72
pndcck
  • 31
  • 1
  • 4
  • find Dropdown in GridView's RowBoundEvent and set SeletedValue of Dropdown. Or you can use SelectedValue='EVAL("ColumnName")' for binding value from GridView's DataSource. – Mayur Borad May 09 '13 at 10:04

3 Answers3

1

In the datagrid view row data bound event do this

 if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRowView dRowView1 = (DataRowView)e.Row.DataItem;
            if ((e.Row.RowState= DataControlRowState.Edit) > 0)
            {

              DropDownList YourdropDown = (DropDownList)e.Row.FindControl("YourdropDown") as DropDownList;
                if (YourdropDown!=null){
              YourdropDown.SelectedValue = dRowView1["ID"].ToString();
                 }
            }
         }
abidmix
  • 1,748
  • 1
  • 16
  • 26
1

Markup:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="ObjectDataSource1" OnRowDataBound="GridView1_RowDataBound">
        <Columns>
            <asp:CommandField ShowEditButton="True" />
            <asp:CommandField ShowSelectButton="True" />
            <asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" />
            <asp:BoundField DataField="Firstname" HeaderText="Firstname" SortExpression="Firstname" />
            <asp:BoundField DataField="Lastname" HeaderText="Lastname" SortExpression="Lastname" />
            <asp:TemplateField HeaderText="Age">
                   <ItemTemplate>
                       <%#Eval("Age") %>
                   </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList runat="server" ID="ddlAge">
                        <Items>
                            <asp:ListItem Text="10" Value="10"></asp:ListItem>
                            <asp:ListItem Text="20" Value="20"></asp:ListItem>
                            <asp:ListItem Text="30" Value="30"></asp:ListItem>
                            <asp:ListItem Text="40" Value="40"></asp:ListItem>
                            <asp:ListItem Text="50" Value="50"></asp:ListItem>
                            <asp:ListItem Text="60" Value="60"></asp:ListItem>
                            <asp:ListItem Text="70" Value="70"></asp:ListItem>
                            <asp:ListItem Text="80" Value="80"></asp:ListItem>
                            <asp:ListItem Text="90" Value="90"></asp:ListItem>
                        </Items>
                    </asp:DropDownList>
                </EditItemTemplate>
            </asp:TemplateField>
        </Columns>

    </asp:GridView>
    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetAll" TypeName="OdsSelectedItem.App_Data.StudentsBll"></asp:ObjectDataSource>

Code:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Student item = e.Row.DataItem as Student;
            if (item != null)
            {
                var ddl = e.Row.FindControl("ddlAge") as DropDownList;
                if (ddl == null) return;
                ddl.SelectedValue = item.Age.ToString();
            }

        }
    }

poor example, but i think it shows into the right direction :)

chris vietor
  • 2,050
  • 1
  • 20
  • 29
0

I had a similar issue just today. my issue was i need to bind the selected value from a list of names to the dropdown. but i had to pass a "selected value" to the obj datasource to populate the dropdown.

you cannot Bind "selectedValue" and use it to pass data to the function that would populate that dropdown.

This is my solution:

             <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="ObjectDataSource1" DataTextField="Text" DataValueField="Value" ToolTip='<%#Eval("ID") %>' SelectedValue='<%# Bind("ManagerID") %>'>
                            </asp:DropDownList>
                            <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="getallIDs" TypeName="MyClass" OldValuesParameterFormatString="original_{0}">
                                <SelectParameters>
                                    <asp:ControlParameter ControlID="DropDownList1" Name="ID" PropertyName="ToolTip" Type="Int32" />
                                </SelectParameters>
                            </asp:ObjectDataSource>
Dannie Adia
  • 71
  • 1
  • 2