C# .NET - how to get the values of Dropdownlist Control from Gridview

Asked By Lata on 25-Apr-11 02:01 AM
Hi All
         In Gridview i have inserted the Dropdown list control i can access that into my cs page but its taking only start value of the dropdown list how can i access the other values from that..please do help me.....my code is

<asp:Label ID="age" runat="server" Text="Age"></asp:Label>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:DropDownList ID="drop1" runat="server" >

In cs page:
 protected void submit_click(object sender,EventArgs e)

      foreach (GridViewRow row in gdview.Rows)
        // Selects the text from the TextBox

        // which is inside the GridView control

       string dropDownListText = ((DropDownList)row.FindControl("Drop1")).SelectedItem.Value;
Reena Jain replied to Lata on 25-Apr-11 02:07 AM

Just make sure that you are only binding your gridview at once in PageLoad with !Page.IsPostback method and try to specify the index of rows and cells when accessing it inside the Grid.

Then try this

DropDownList ddl = (DropDownList)GridView1.Rows[e.RowIndex].Cells[0].FindControl("DropDownList1");
string val = ddl.SelectedValue;

Hope this will help you
TSN ... replied to Lata on 25-Apr-11 02:26 AM

Your Question is unclear, you did not mention whether to take the selected value of dropdown list of the only seleted row or you want to take the dropdownlist value from every row...

Ravi S replied to Lata on 25-Apr-11 02:34 AM

Follow this code

DropDownList drdList;

foreach (GridViewRow grdRow in GridView1.Rows)

    drdList = (DropDownList)( GridView1.Rows[ grdRow.RowIndex ].Cells[1].FindControl( "DropDownList1" ));

    // DataBinding of nested DropDownList Control for each row of GridView Control.
    drdList.DataSource = myDataSet;
    drdList.DataValueField = "order_status_id";
    drdList.DataTextField = "order_status_name";
    drdList.SelectedValue = GridView1.DataKeys[grdRow.RowIndex]["order_status_id"].ToString();

Refer the link below

Hope it helps you...
Ravi S replied to Lata on 25-Apr-11 02:34 AM

refer to this link also

Lata replied to TSN ... on 25-Apr-11 02:40 AM
Hi Sainath

        In Gridview control Dropdownlist is there from dropdownlist i want values.....
TSN ... replied to Lata on 25-Apr-11 02:45 AM

you the below code making necessary changes with your requirement,...

 foreach (GridViewRow r in GridView2.Rows )
DropDownList ddl=(DropDownList)r.FindControl("DropDownListId");
string selectedValue=ddl.SelectedValue;
TSN ... replied to Lata on 25-Apr-11 02:48 AM
check in the page Load whether you are checking for PostBack or Not..

i.e if(!IsPostBack)
     //bind gridView Here only...
Lata replied to TSN ... on 25-Apr-11 02:51 AM
 Hi thanks for reply,

      here also i am getting only first value of the gridview...i cant access other values of dropdown list
Lata replied to TSN ... on 25-Apr-11 02:53 AM
thank you so much sainath....i got the answer.........
collo7ns Adell replied to Lata on 19-Aug-13 05:57 AM
DropDown List Control with C# Code tutorial, http://www.kettic.com/winforms_ui/dropdown_list.shtml