Click here to Skip to main content
15,914,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a gridview, in which one column has been transoformed to TemplateField that likes this:
XML
<asp:TemplateField HeaderText="name" SortExpression="name">
    <EditItemTemplate>
        <asp:TextBox ID="TextBox_Name" runat="server" Text='<%# Bind("name") %>' </asp:TextBox>
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label ID="Label_Name" runat="server" Text='<%# Bind("name") %>'></asp:Label>
    </ItemTemplate>
 </asp:TemplateField>
<asp:TemplateField HeaderText="id" SortExpression="id">
    <EditItemTemplate>
        <asp:TextBox ID="TextBox_ID" runat="server" Text='<%# Bind("id") %>' </asp:TextBox>
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label ID="Label_ID" runat="server" Text='<%# Bind("id") %>'></asp:Label>
    </ItemTemplate>
 </asp:TemplateField>
 <asp:TemplateField HeaderText="sex" SortExpression="sex">
    <ItemTemplate>
         <asp:Label ID="Label_Sex" runat="server" Text='<%# Bind("sex") %>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
         <asp:DropDownList ID="DropDownList_Sex" runat="server" >
             <asp:ListItem>Male</asp:ListItem>
             <asp:ListItem>Female</asp:ListItem>
             </asp:DropDownList>
    </EditItemTemplate>
 </asp:TemplateField>



If I load a record from DB, for example :
C#
ID      Name             Sex                    
098     Mary Loen        Sorry, It is a secret.


When I edit this record in the gridview's updating event, the displayed dropdownlist's selectedvalue is not the record "Sorry, It is a secret.", but the first item - "Male".

The C# code is:
C#
protected void GridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
   DataTable dt = new DataTable();
   dt = (DataTable)Cache["dataTable"];
   dt.PrimaryKey = new DataColumn[] { dt.Columns["id"] };
   DataRow dr = dt.Rows.Find(Session["ID"].ToString());

   TextBox tb = (TextBox)GridView.Rows[e.RowIndex].FindControl("TextBox_Name");
   dr["name"] = tb.Text.Trim();

   drdList = (DropDownList)(GridView.Rows[e.RowIndex].Cells[1].FindControl("DropDownList_Sex"));
   dr["sex"] = drdList.SelectedValue;

   GridView.EditIndex = -1;
   FillData2Grid();
}
	
protected void GridView_RowEditing(object sender, GridViewEditEventArgs e)
{
   GridView.EditIndex = e.NewEditIndex;
   FillData2Grid();
   TextBox tb = (TextBox)GridView.Rows[e.NewEditIndex].FindControl("TextBox_ID");
   Session["ID"] = tb.Text;
}


How could I make the dropdownlist show the original selectedvalue ?
Posted
Updated 24-Oct-12 6:31am
v2

In Page_Load add to dropdownlist Sorry it's a secret, what you load from a database and select it
 
Share this answer
 
Hi,

Try this if could help..

In your page load event find the index position of your dropdown list.

Example index position of Female:
0 = "--Select One--"
1 = "Male"
2 = "Female"

C#
this.ddlGender.SelectedIndex = 2;


The above code will display your original selected value which is Female.

Please vote if could help...

Regards,
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900