Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi All...

I am using Dorp-down list in edit item template of grid-view, while updating it is not retreiving the selected-item of the drop-down list instead its retreiving the first list item of drop-down list.

following is the code for .aspx page:-
XML
<asp:TemplateField HeaderText="Status">
                           <EditItemTemplate>
                            <center>   <asp:DropDownList ID="ddlStatus" runat="server">

                                   <asp:ListItem>Dispached</asp:ListItem>
                                   <asp:ListItem>Delivered</asp:ListItem>
                                   <asp:ListItem >Pending</asp:ListItem>
                               </asp:DropDownList></center>
                           </EditItemTemplate>
                           <ItemTemplate>
                                <asp:Label ID="Label6" runat="server" Text='<%# Eval("OStatus") %>'  ForeColor="Red"></asp:Label>
                                </ItemTemplate>
                       </asp:TemplateField>



following is the code of aspx.cs for gridview_rowupdating:-

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
   {
       string id = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
       DropDownList ddlstatus = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlStatus");
       string sd = ddlstatus.Text;
       SqlCommand cmdUpdate = new SqlCommand("Update Orders set OStatus = '" + status.SelectedItem.Text + "' where OID='" + id + "'", con);
       cmdUpdate.ExecuteNonQuery();
       con.Close();
       GridView1.EditIndex = -1;
       databind();
   }
Posted

Replace
C#
ddlstatus.Text
with
C#
ddlstatus.SelectedValue
or
C#
ddlstatus.SelectedItem.ToString()
 
Share this answer
 
Comments
ujjwal uniyal 3-Feb-12 3:23am    
Have tried all three of them but still it's not retrieving the value selected instead it's retrieving the first list item
C#
ddlstatus.selecteditem.text
or
ddlstatus.selectedtext
 
Share this answer
 
try this
C#
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
 {
  GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
  string id = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
  string ddluserType = ((DropDownList)row.FindControl("ddlStatus")).SelectedValue;
  SqlCommand cmdUpdate = new SqlCommand("Update Orders set OStatus = '" + ddluserType + "' where OID='" + id + "'", con);
  cmdUpdate.ExecuteNonQuery();
  con.Close();
  GridView1.EditIndex = -1;
  databind();
 }
 
Share this answer
 
v4
Comments
Anuja Pawar Indore 3-Feb-12 8:49am    
Formatting done
C#
for(int i=0;<gridview1.rows.count;i++)>
{
GridViewRow gvr=GridView1.Rows[i];
string strControl = gvr.FindControl("ddlStatus") as DropDownList;
string Value=strControl.SelectedItem.Text.ToString();
}
 
Share this answer
 
v3
Comments
ujjwal uniyal 3-Feb-12 6:15am    
it's still retrieving the first list item of the dropdownlist not the selected item of the dropdownlist.
Anuja Pawar Indore 3-Feb-12 8:49am    
Added pre tag
C#
// here You get Dropdown and gridview cell in which dropdown is present



DropDownList ddlStatus = (DropDownList)grdViewSIPSummary.Rows[rowIndex].Cells[2].FindControl("ddlStatus");


//Get Selected text
ddlStatus.SelectedItem.Text.ToString();




Cells[2] is important.
means at which cell number your dropdown is!
starting with zero.
 
Share this answer
 
make DropDownlist property AutoPostBack="True"
 
Share this answer
 
v2
make DropDownlist property AutoPostBack="True"

try it definatly it will work
 
Share this answer
 
its not necessary to mention cell number to find the control of the gridview
 
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