473,396 Members | 1,756 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

drop down list in databound gridview help?

Hi,
I am using a gridview to show information from a database

nameid | name | address1 | postcode | countryid
1 | bob | 24 green street | ga22ac | 2



and a country table

countryid | countryname

1 | USA
2 | United Kingdom



the user table is shown in the gridview as a series of text boxes,but instead of a 2 for countryid as a textbox, i'd like it to be a drop down list with either USA, United Kingdom, or anything else from the country table column so that its possible to change this value.

for the life of me i cant seem to figure it out. please could someone show me the light !

thanks alot

P&P :-)
Nov 26 '07 #1
2 1505
dip_developer
648 Expert 512MB
Hi,
I am using a gridview to show information from a database

nameid | name | address1 | postcode | countryid
1 | bob | 24 green street | ga22ac | 2



and a country table

countryid | countryname

1 | USA
2 | United Kingdom



the user table is shown in the gridview as a series of text boxes,but instead of a 2 for countryid as a textbox, i'd like it to be a drop down list with either USA, United Kingdom, or anything else from the country table column so that its possible to change this value.

for the life of me i cant seem to figure it out. please could someone show me the light !

thanks alot

P&P :-)
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.Populate your country DropDownList using custom method PopulateDropDownList().

Here is the method :

Expand|Select|Wrap|Line Numbers
  1.  
  2. public DataSet PopulateDropDownList() 
  3. {
  4. SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString);
  5. SqlDataAdapter myAdapter = new SqlDataAdapter("SELECT * FROM tblCountry", myConnection);
  6. DataSet myDataset = new DataSet();
  7. myAdapter .Fill(myDataset , "tblCountry");
  8. return myDataset ; 
  9. }
  10.  
Now call this method like this :

Expand|Select|Wrap|Line Numbers
  1.  
  2. <ItemTemplate>
  3. <asp:DropDownList ID="DropDownList1" runat="server" DataSource="<%# PopulateDropDownList() %>" 
  4. DataTextField="countryname" DataValueField = "countryid">
  5. </asp:DropDownList>
  6. </ItemTemplate>
  7.  
Nov 26 '07 #2
brilliant!, thanks alot, are there any other methods anyone could recommend, i'd like to email this on to someone to have a read up on.

thanks again!
Nov 26 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Bishop | last post by:
I made a GridView with a Drop Down List using the example here: http://www.orcsweb.com/articles/dotnet_control_templates.aspx It appears to work except that it dosn't actually save the updated...
0
by: thebison | last post by:
Hi all, I hope someone can help with this relatively simple problem. I am building a timesheet application using ASP.NET C# with Visual Studio 2003.As it is only a protoype application, my...
0
by: ayneekeaw | last post by:
When I click the update button/link at the Gridview to update the data from drop down list into the SQL. The selected value of drop down list change to default value. How can I fix this problem
0
by: David | last post by:
I cannot get a simple changing of a SelectCommand for a SqlDataSource used in a drop-down list to work. If I do the same process to my GridView SqlDataSource it works fine. Can anyone help? I am...
0
by: Derty | last post by:
Have an issue with a simple drop down list that I need to filter results of a database table and page display. I can get the drop down list to populate correctly but can't make the page databound...
2
by: McGeeky | last post by:
Hi. I have a read only GridView. For each row I want to display a drop down list which contains a list of actions the user can select from for that particular row. E.g. "view details". When they...
0
by: yosri2005 | last post by:
Hello, I'm sure many of you have seen the error message in the subject. I found quiet a few posts on the web regarding this issue, but the ones I saw mainly tackle this issue when you have a...
5
by: Mark B | last post by:
I'd like to have a field in a gridview (or standalone on a webpage) that not only drops down each salespersons name but also precedes their name with a blue, red, green or orange dot icon depicting...
3
by: Slickuser | last post by:
Hi, I would like to put a drop down menu in a gridview with the datatype selected from the SQL database. These are the values will show. red orange white green
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.