Link to home
Start Free TrialLog in
Avatar of vmaddalis
vmaddalis

asked on

Dropdownlist in Gridview not storing or getting selected value

Hi,
I have a dropdownlist in a gridview for the edit mode. The problem is when I click the edit button the dropdownlist's selected value is always the first value of the dropdownlist's items.
And also while updating it always updates with the first value of the dropdownlist eventhough I have selected some other value.
In aspx:
***********************
<asp:GridView id="gvTasks" OnRowCommand="gvTasks_RowCommand" OnRowUpdating="gvTasks_RowUpdating" OnRowDataBound="gvTasks_RowDataBound" DataKeyNames="pkiTask" OnRowEditing="gvTasks_RowEditing" AutoGenerateColumns="False" CellPadding="2">
  <Columns>
    <asp:ButtonField DataTextField="uqsTaskName" HeaderText="Task Name" Text="Button" SortExpression="uqsTaskName" CommandName="ViewMessage" />
    <asp:TemplateField HeaderText="Assign To" >
      <EditItemTemplate >
       <asp:DropDownList ID="ddlLogins" runat="server" Width="190px" TabIndex="-1" DataValueField="pksLogin" 
                            DataTextField="pksLogin">
       </asp:DropDownList>
      </EditItemTemplate>
      <ItemTemplate>
         <asp:Label ID="Label1" runat="server" Text='<%# Bind("sAssignedTo") %>'></asp:Label>
      </ItemTemplate>
   </asp:TemplateField>
 </Columns>
</asp:GridView>
 
In Code behind aspx.cs
*******************************
protected void gvTasks_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DBServices DBS = new DBServices();
        int roleID = (int)Session["RoleID"];
        DataSet dsInit = new DataSet();
        dsInit = DBS.GetUserSecuritysLogin(roleID);
        DropDownList ddl = (DropDownList)e.Row.FindControl("ddlLogins");
        if (ddl != null)
        {
            if (dsInit.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in dsInit.Tables[0].Rows)
                {
                    ddl.Items.Add(dr["pksLogin"].ToString());
                }
            }
            //string loginName = e.Row.Cells[3].Text;
            ddl.SelectedValue = e.Row.Cells[3].Text;   
        }
    }
 
protected void gvTasks_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
               
        int index = e.RowIndex;
        GridViewRow row = gvTasks.Rows[index];
        
        int TaskID = (int)gvTasks.DataKeys[row.DataItemIndex].Value;
 
        DropDownList ddl1 = new DropDownList();
        ddl1 = (DropDownList)gvTasks.Rows[index].FindControl("ddlLogins");
        string loginName = ddl1.SelectedValue;
 
        DBServices DBS = new DBServices();
        DBS.SetTasksAssignedTo(TaskID, loginName);
 
        gvTasks.EditIndex = -1;
        BindData();
 
    }

Open in new window

Avatar of Munawar Hussain
Munawar Hussain
Flag of Pakistan image

you need to reselect the previously selected item in ItemDataBound event after you rebind the DataGrid

1 click the edit button
2 it will go to edit mode .. and will fill the textboxes and dropdowns..
after you fill the dropdownlist.. .. get your previously selected item (from database of viewstate if you save there before or whereever you have previosuly selected item)

Thanks
Avatar of vmaddalis
vmaddalis

ASKER

I didn't understand on how to get the previously selected item. Can you explain with code?
I placed selectedvalue in the EditItemTemplate's dropdownlist but it is giving an error. I changed 500 points for this question?

<asp:TemplateField HeaderText="Assign To" >
      <EditItemTemplate >
       <asp:DropDownList ID="ddlLogins" runat="server" Width="190px" TabIndex="-1" DataValueField="pksLogin"
                            DataTextField="pksLogin" SELECTEDVALUE='<%# Bind("sAssignedTo") %>'>
       </asp:DropDownList>
      </EditItemTemplate>
      <ItemTemplate>
         <asp:Label ID="Label1" runat="server" Text='<%# Bind("sAssignedTo") %>'></asp:Label>
      </ItemTemplate>
   </asp:TemplateField>
ASKER CERTIFIED SOLUTION
Avatar of altaf_techie
altaf_techie
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Forced accept.

Computer101
EE Admin