Accessing DropDownList inside GridView Control
By AzamSharp
Views: 9952

 

Introduction:

In the last article Selecting Checkboxes inside the GridView Control we saw that how we can select single as well as multiple checkboxes. Checkboxes are not the only control that can be inside the GridView. You can literally place most of the controls in it. In this article I will show you how you can populate a dropdownlist which resides in the GridView control.

Populating DropDownList inside the GridView:

There are various techniques you can use to populate a DropDownList inside the GridView control. The most simple is using the SqlDataSource object. Simply assign the DataSource property of the DropDownList to the SqlDataSource object through the smart tag and that's it. In this article we will be populating DropDownList using our custom method.

// populates the dropdownlist

public DataSet PopulateDropDownList()

{

SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString);

SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM tblPhone", myConnection);

DataSet ds = new DataSet();

ad.Fill(ds, "tblPhone");

return ds;

}

Now let's see where we call this method.

 <ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataSource="<%# PopulateDropDownList() %>"
DataTextField="Phone" DataValueField = "PhoneID">
</asp:DropDownList>
</ItemTemplate>

Now let's see how we can iterate through the GridView and get the selected value of the DropDownList.

protected void Button2_Click(object sender, EventArgs e)

{

StringBuilder str = new StringBuilder();

foreach (GridViewRow gvr in GridView1.Rows)

{

string selectedText = ((DropDownList)gvr.FindControl("DropDownList1")).SelectedItem.Text;

str.Append(selectedText);

}

Response.Write(str.ToString());

}

We simply iterates through the GridView control using GridViewRow object and find the "DropDownList1" control. Than we perform casting the row into the DropDownList and accessing its SelectedItem.Text property.

I hope you like this article, happy coding!

 

By AzamSharp


Enter Comment/Feedback
  •  
  •  
  •  
  •  
  •  

Comments/Feedbacks
Subject: No getting selected va
Name: Dheemluean
Date: 2/5/2007 11:11:57 PM
Comment:
I cannot access the selected field on button click as the page is posting bak.. I've put the Load of the grid inside if(!page.IsPostBack) ..
Can u pls suggest?
Subject: access dropdownlist inside EditItemTemplate.
Name: Prashant
Date: 2/26/2007 8:52:52 PM
Comment:
Hi,

i have a gridview column in which i show a label in the view mode which can be either True or False.

In the edit mode i want to display it as a dropdown list.

The problem is i cannot find the dropdown using gvRow.FindControl("ddl").

Can you help with this.

Thanks
Prashant
Subject: i have some doubt in this article
Name: subhranshu
Date: 4/3/2008 2:45:02 AM
Comment:
i can not understand why it has written two select statements.
Subject: RE: I have some doublt
Name: AzamSharp
Date: 4/3/2008 2:34:55 PM
Comment:
Hi Subhranshu, Can you please explain what you meant?
Subject: suggestion
Name: aaryan
Date: 4/3/2008 10:56:25 PM
Comment:
Hi,your articles r very simple and productive.
Why don't you add a search option to ur site?
Subject: mouseover-servercontrol
Name: aaryan
Date: 4/6/2008 12:21:29 AM
Comment:
Hi,ihave a txtbox & a button.I want a calendar to get visible on mouseover the button,then select the date for txtbox from the calender.
Can u plz.suggest any help?



Join WebHost4Life.com






Copyright GridViewGuy 2007-2008