Click here to Skip to main content
6,306,412 members and growing! (19,662 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Beginner License: The Code Project Open License (CPOL)

Nesting the DropDownList to Gridview in ASP.NET 2.0 to update

By chjk

Nesting the DropDownList to Gridview in ASP.NET 2.0 to update a column
C# (C# 2.0), Javascript, CSS, HTML, .NET (.NET 2.0), ASP, ASP.NET, Ajax, Dev
Posted:27 Mar 2008
Views:13,157
Bookmarked:10 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
2 votes for this article.
Popularity: 0.30 Rating: 1.00 out of 5
2 votes, 100.0%
1

2

3

4

5

Introduction

In this article I will demonstrate how to Nest the DropDownList to Gridview in asp.net 2.0 to update data.ITemplate will need to be created that can be used for representing and editing the data within the Gridview . i use a label in the ItemTemplate to represent the City column and a DropDownList in the EditItemTemplate to edit it .

Background

When i use Gridview i ask myself can i Nest DropDownList to Gridview to update data,now i make it ,i share this pleasure with everyone!

Step By Step

1、 Binding Data to Gridview from SqlDataSource1,DataSource is (Northwind--Customers[CustomerID、CompanyName、City、ContactName]),i add a new table City[CityId,City] into the Northwind.as shown below: 2、Add one TemplateField column to the GridView,add a label in the ItemTemplate field and Design as shown below: 3.1、now add a DropDownList in the EditItemTemplate field and Set its DataSource to SqlDataSource2(Northwind--City[City]).shown below: 3.2、and click "choose data source..." Configure it as shown below4: 3.3、and click "Edit DataBindings..." Configure it as shown below5: 4、the complete code of Default2.aspx see DataBind.zip.

Using the code

Configure SqlDataSource1 like this:

SelectCommand="SELECT [CustomerID], [CompanyName], [City], [ContactName] FROM [Customers]"
UpdateCommand="UPDATE [Customers] SET [CompanyName] = @CompanyName, [City] = @City, [ContactName] = @ContactName WHERE [CustomerID] = @CustomerID">
					

Configure SqlDataSource2 like this:

<asp:SqlDataSource ID="SqlDataSource2"  runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [City] FROM [City]">
                         
					

Configure DropDownList like this:

DropDownList ID="DropDownList1" runat="server" Width="104px"  SelectedValue='<%# Bind ("City") %>' DataSourceID="SqlDataSource2" DataTextField="City" DataValueField="City"
					

Remember to add a new table City[CityId,City] into the Northwind by yourself.

Points of Interest

By the way,i complete this case without writing a line code. the vs.net2005 Generat the code Automaticly.

History

This is my first Article in codeproject!

License

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

About the Author

chjk


Member
A student Who like Coding and enjoy it.
I am interested in C#、ASP.NET、Java, now have been studying Ajax and Silverlight.
But my favorite is XML Web service。
when i don't do Programming, i will listen Rock N Roll (i like metallica very much )and paragliding ,yes!paragliding is so cool!
Occupation: Web Developer
Company: FuJian normal university
Location: China China

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
Generalhelp Pinmembermerinkmathew18:52 19 Feb '09  
GeneralMy vote of 1 PinmemberJoe Gakenheimer )2:34 16 Dec '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 27 Mar 2008
Editor:
Copyright 2008 by chjk
Everything else Copyright © CodeProject, 1999-2009
Web11 | Advertise on the Code Project