简体   繁体   中英

Drop down list is not updated in the gridview

I have a gridview populated with elements extracted from a table in the database. Each row contains 2 textboxes and 1 drop down list. The drop down list is filled when the page is loaded.

My issue is: when I edit the row, select another item from the drop down list and then click on update button, nothing changes. The drop down list still returns to its default value, neither the modiefied values are updated in the db (delete doesn't work etc). I can't understand the reason. Please help me.

My Gridview:

<asp:GridView ID="MyGridView" runat="server" AutoGenerateColumns="false"    OnRowDataBound="GridView_OnRowDataBound" OnRowUpdating="GridView_OnRowUpdating" 
 DataKeyNames="id" DataSourceID="DataSource" ><AlternatingRowStyle BackColor="White" />
<Columns>
    <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowCancelButton="True" />

 <asp:TemplateField HeaderText="currentCity" SortExpression="currentCity" ItemStyle-  HorizontalAlign="Center" Visible="false">
        <ItemTemplate>
            <asp:Label runat="server" ID="lblcurrentCity" Text='<%#   Bind("CodiceContrattoRisorsa") %>' Visible="false" ></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="id" SortExpression="id" ItemStyle-HorizontalAlign="Center" >
        <ItemTemplate>
            <asp:TextBox runat="server" ID="txtId" Text='<%# Bind("id") %>'></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>

 <asp:TemplateField HeaderText="name" SortExpression="name" ItemStyle-  HorizontalAlign="Center" >
        <ItemTemplate>
            <asp:TextBox runat="server" ID="txtName" Text='<%# Bind("name") %>'>    </asp:Label>
        </ItemTemplate>
    </asp:TemplateField>


    <asp:TemplateField HeaderText="city" SortExpression="city" ItemStyle-  HorizontalAlign="Center">
        <EditItemTemplate>
            <asp:DropDownList ID="ddlCity" runat="server"></asp:DropDownList>
        </EditItemTemplate>
        <ItemTemplate>
            <asp:DropDownList ID="ddlCity" runat="server" Enabled="false"  >
            </asp:DropDownList>
        </ItemTemplate>
    </asp:TemplateField>

 </Columns>
 <RowStyle BackColor="#EFF3FB" />
 <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
 </asp:GridView>

 <asp:EntityDataSource ID="DataSource"
    runat="server" ContextTypeName="Context"
    EntitySetName="users" EntityTypeFilter="user"
    EnableDelete="True" EnableUpdate="True" />

Loading page....

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            MyGridView.DataBind();
    }

protected void GridViewCausali_OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // initialize ddl in gridview
            DropDownList ddl = (DropDownList)e.Row.FindControl("ddlCity");
            List<city> cities = new List<city>();
            cities = GetFromDB();

            ddl.DataSource = cities;
            ddl.DataBind();

            ddl.Items.FindByValue((e.Row.FindControl("lblcurrentCity") as                                                     
            Label).Text).Selected = true;

        }
    }

protected void GridView_OnRowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        DropDownList ddl = (DropDownList)MyGridView.Rows[e.RowIndex].FindControl("ddlCity");
        string test = ddl.SelectedValue;
        Label lbl = (Label)MyGridView.Rows[e.RowIndex].FindControl("lblcurrentCity");
        lbl.Text = ddl.SelectedValue;
        ddl.Items.FindByValue((MyGridView.Rows[e.RowIndex].FindControl("lblcurrentCity") as Label).Text).Selected = true;
                }

I suggest you to add DefaultContainerName property , add stringconnection

<asp:EntityDataSource ID="DataSource" runat="server" 
    ConnectionString="name=..."
    DefaultContainerName="..." 

    EntitySetName="users" 
    EnableDelete="True" EnableInsert="True" EnableUpdate="True"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM