[RESOLVED]How to update a value in gridview using DropDownList?

I have populated the list items using Sqldatasource from my database, but I’m not sure how do I use the selected item to update it?

Here’s what I have

<asp:TemplateField HeaderText="Game Title" SortExpression="title">
                                <EditItemTemplate>
                                    <asp:DropDownList ID="ddlOrderTitle" runat="server"
                                        DataSourceID="SqlDataSource1" DataTextField="title" DataValueField="title" SelectedValue='<%# Eval("title") %>'></asp:DropDownList>
                                    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
                                        ConnectionString="<%$ ConnectionStrings:GameConnectionString %>"
                                        SelectCommand="SELECT [title] FROM [games]"></asp:SqlDataSource>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblOrderTitle" runat="server" Text='<%# Bind("title") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>

And here’s what I have currently

 protected void grdOrder_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //get selected row
        int selectedRow = e.RowIndex; 

        //get order ID from data key
        int orderID = (int)grdOrder.DataKeys[selectedRow].Value;

        //get current grid view row
        GridViewRow row = (GridViewRow)grdOrder.Rows[selectedRow];

        //find text box for txtOrderQuantity
        TextBox quantity = (TextBox)row.FindControl("txtOrderQuantity");
        

        string strQuantity = quantity.Text;
        double intQuantity = 0;
        //double intQuantity = Convert.ToInt32(quantity.Text);

        if (Double.TryParse(strQuantity, out intQuantity))
        {
            updateOrderGridViewRecord(orderID, intQuantity);
        }
        else
        {
            lblOrderError.Text = "Invalid quantity!";

        }
    }

I have an event called grdOrder_RowUpdating to update my quantity value from a textbox.  How do I do it if it’s a dropdownlist instead?

When using SqlDataSource no need code behind, simply bind it like you bind textbox, SelectedValue=’<%# Bind("title") % >’

Leave a Reply