Videos | Podcasts

Accessing DropDownList inside GridView Control
AzamSharp
Published Date: 7/23/2005 9:42:21 PM
Views: 18930

Abstract:
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.

 

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!

 




Did you like this article?
kick it on DotNetKicks.com Submit
Similar Articles

Solving CheckAll Problems for GridView with CheckBoxes in Multiple Columns

Solving Multiple GridViews CheckBox Selection Problem

Exporting GridView to PDF Document

Nested GridView with TreeView Like Structure

Updating Only Changed Rows in the GridView Control

Enter Comment/Feedback

 
 
 
 
 

Comments/Feedbacks