Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unique Values In Drop Down List 1

Status
Not open for further replies.

Aidy680

Technical User
Nov 1, 2006
138
0
0
GB
Hi,

I have a datatable dt. Within this datatable are numerous fields including 'Name' and 'ID'. Neither fields are unique.

I want to bind this datatable to my dropdownlist ddlExample, only displaying unique 'Name' values. I want the 'ID' field to be the value of the drop down list.

Can this be done?

So far, I've got the following:

var query = (from row in dt select row.Name).Distinct();
ddlExample.DataSource = query;
ddlExample.DataValueField = null;
ddlExample.DataTextField = null;
ddlExample.DataBind();

which returns unique 'Name' values, but I cant capture the'ID' value.

I tried updating the code to read:

ddlExample.DataValueField = "ID";

but it didnt work.

Any help would be appreciated.


 
you need to select distinct ID and Name from the database. To show a distinction between Names you may want to display the Id in the text value as well something like

value = ID
text = Name (ID)

Jason Meckley
Senior Programmer

faq855-7190
faq732-7259
My Blog
 
Thanks Jason but when I try to update the SELECT string with something like:

var query = (from row in dt select row.Name, row.ID).Distinct();

it wont allow it.

If I can just get this syntax right, I think I'm in business!
 
what is the name of "ID" column in database? is the ID column mapped?

Jason Meckley
Senior Programmer

faq855-7190
faq732-7259
My Blog
 
It is just called 'ID'. This is what it's named in the DataTable also.

Interestingly, if I place it before row.Name, like so:

var query = (from row in dt select row.ID,row.Name).Distinct();

it accepts it, but now doesn't like row.Name!

Maybe something to do with row??

 
Ah, now I see it. you need to create an anonymous object.
[tt]
var query = (from row in dt select new {row.ID, row.Name}).Distinct();
[/tt]

Jason Meckley
Senior Programmer

faq855-7190
faq732-7259
My Blog
 
Result! It works! Many thanks Jason. This site has come up trumps again.

As an aside, I dont find .NET anywhere near as intuitive as VBA is. Hopefully I'll get used to it.
 
it's a different language, vba is a scripting language where .net is a compiled language. there are strengths and weaknesses of each.

try to resist the urge to treat .net like VBA. It's difficult because we default to what we know and assume it should be the same, but it's not. the same would be true if you were moving to javascript, ruby, java, etc. there are similar constructs and ideas, but how to capitalize on them is different in each language.

Jason Meckley
Senior Programmer

faq855-7190
faq732-7259
My Blog
 
Yes I'm sure it would be just as painful moving to any other new environment.

It's an experience thing - the more I use this, the more examples of where it is superior to VBA, I'll find.

Thanks again for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top