Advertisement

01.24.2008 at 03:54AM PST, ID: 23107347
[x]
Attachment Details

Dropdownlist in Gridview not storing or getting selected value

Asked by vmaddalis in Programming for ASP.NET

Tags: ASP.NET, C#

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.Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
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();
 
    }
[+][-]01.24.2008 at 04:05AM PST, ID: 20732177

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.24.2008 at 04:08AM PST, ID: 20732194

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.24.2008 at 08:56PM PST, ID: 20740205

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]02.25.2008 at 04:25AM PST, ID: 20974583

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Programming for ASP.NET
Tags: ASP.NET, C#
Sign Up Now!
Solution Provided By: altaf_techie
Participating Experts: 3
Solution Grade: A
 
 
[+][-]03.20.2008 at 06:27PM PDT, ID: 21177395

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628