GridView编辑模式下下拉列表无法保留原有选中值问题求助
Hey Georgi, let's get that dropdown issue sorted out! The problem is two-fold: your edit-mode dropdowns aren't binding to the record's existing values, and your data query isn't including the necessary fields to make that binding work. Here's how to fix it step by step:
Step 1: Update Your SQL Query to Include Required Fields
First, your userSales() method's SELECT statement doesn't include the C_ID and Product_ID from the SalesActivity table—these are the values we need to bind to the dropdowns in edit mode. Modify your query to add these fields:
SqlCommand cmd = new SqlCommand(@" SELECT SalesActivity.A_ID, SalesActivity.Date, SalesActivity.Quatation_Number, CUSTOMER.Agent_ID, CUSTOMER.Customer_Name, PRODUCTS.Machine_Model, SalesActivity.C_ID, SalesActivity.Product_ID, -- Add these two fields SalesActivity.Quantity, SalesActivity.valueGBR, SalesActivity.valueEUR, SalesActivity.weightedValue, STATUSES.Status, SalesActivity.estDecisionDate, SalesActivity.PromisedDeliveryDate FROM [SalesActivity] INNER JOIN [CUSTOMER] ON SalesActivity.C_ID = CUSTOMER.C_ID INNER JOIN [PRODUCTS] ON SalesActivity.Product_ID = PRODUCTS.Product_ID INNER JOIN STATUSES ON SalesActivity.Status_ID = STATUSES.Status_ID where CUSTOMER.Agent_ID = @Agent_ID", con);
Even though you don't display these fields in the ItemTemplate, GridView needs access to them to set the dropdown's selected value.
Step 2: Fix the EditItemTemplate DropDownList Bindings
Your edit-mode dropdowns are missing the SelectedValue binding that connects them to the row's existing value, and they don't include the default "Select..." option. Update both dropdowns in your EditItemTemplate:
For Customer Dropdown (eddlCName):
<EditItemTemplate> <asp:DropDownList ID="eddlCName" DataSourceID="SqlDataSource5" Width="145px" DataTextField="Customer_Name" DataValueField="C_ID" SelectedValue='<%# Bind("C_ID") %>' AppendDataBoundItems="True" runat="server"> <asp:ListItem Value="-1">---Select Customer---</asp:ListItem> </asp:DropDownList> </EditItemTemplate>
For Product Dropdown (eddlMmodel):
<EditItemTemplate> <asp:DropDownList ID="eddlMmodel" DataSourceID="SqlDataSource4" DataTextField="Machine_Model" DataValueField="Product_ID" Width="138px" SelectedValue='<%# Bind("Product_ID") %>' AppendDataBoundItems="True" Runat="server"> <asp:ListItem Value="-1">---Select Product---</asp:ListItem> </asp:DropDownList> </EditItemTemplate>
SelectedValue='<%# Bind("C_ID") %>'tells the dropdown to match its selected value to the current row'sC_ID(orProduct_IDfor the other dropdown).AppendDataBoundItems="True"ensures your default option stays alongside the datasource items instead of being overwritten.
Step 3: Complete Your RowEditing Method
Your provided GridView1_RowEditing code is cut off—make sure it properly sets the edit index and rebinds the data to load the edit mode correctly:
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; // Rebind the data to refresh the GridView in edit mode userSales(); }
After making these changes, when you click the edit button, the dropdowns should automatically select the value that's already saved in the SalesActivity record instead of resetting to the first option.
内容的提问来源于stack exchange,提问作者Georgi Georgiev

