473,800 Members | 2,608 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

drop down list in databound gridview help?

43 New Member
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 1526
dip_developer
648 Recognized Expert Contributor
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 PopulateDropDow nList().

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
pieandpeas
43 New Member
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
1224
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 data selected by the drop down list. Instead it appears to save the origional data. Any idea what I might be doing wrong?
0
1516
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 database has been made in MSDE. My question is simple, on my Projects form I have a 'search' button, which when clicked re-binds a datagrid with a RowFilter based on a number of user-input parameters. Two of these parameters are drop-down
0
2648
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
931
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 using the simple line of code (below) at the top of the aspx web page: 'SqlIssue.SelectCommand = "SELECT 0 As issue_seq_nbr, '--- Not Selected ---' As issue_display UNION SELECT " & intid & " As issue_seq_nbr, '" & dtra("issue_name") & "' As...
0
1632
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 items change with selection. I've included my code below. Any help is appreciated: <%@ Page language="C#" %>
2
14394
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 select an action in the drop down list I want to do a post back. How will I know which row they made the selection in? Is there a way to embed the row Id in the drop down list so it gets sent back to the server? Thanks
0
2359
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 drop-down list depending on another drop-down list within a form view. This is not what I'm trying to do, so I didn't know how to apply the solutions I found to my problem. I have a formview which inserts items into a database table via an
5
1709
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 what group they belong to. Can drop-downs have such graphics as opposed to just text? TIA
3
1863
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
9553
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10509
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10281
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10256
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10039
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6824
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5612
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3765
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2953
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.