473,434 Members | 1,559 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,434 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 1506
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...
1
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...
0
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.