0
votes

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;
                }
1
Where is your Rowupdating event? is the ddl set to autopostback false? - briskovich
yes, autopostback is set to false. I edit the onRowUpdating event - Camilla
there are several events you have to address in this process. Post the code for your rowUpdating event. You have a RowEditing event, RowUpdating and rowUpdated. I think your issue is in the updating event. - briskovich
You should really do everything for a datagrid from your code behind file. You get the most control and it is so much easier. - briskovich

1 Answers

0
votes

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"/>