Re: Removing duplicates from a DropdownList

B

brian richards

Well I got it to work like this:

public void BindGrid()
{
UpdateSelectStmt();
adptr_LocationSummary.Fill(dsLocationSummary1);
CityChooser.DataSource = dsLocationSummary1;
CityChooser.DataTextField = "City";
CityChooser.DataValueField = "City";
CityChooser.DataBind();
for(int i = 0;i<CityChooser.Items.Count;i++)
{
while( (i+1) < CityChooser.Items.Count && CityChooser.Items.Value ==
CityChooser.Items[i+1].Value)
{
CityChooser.Items.RemoveAt(i+1);
}
}
dg_LocationSummary.DataBind();
}

I wasn't comparing the values before. But this doesn't feel like a very
good solution. Meaning what about when I'm doing this for 8000 cities?

-Brian
 
M

Marina

Why not run a separate query to get the distinct cities? That's really the
way to do it.

brian richards said:
Well I got it to work like this:

public void BindGrid()
{
UpdateSelectStmt();
adptr_LocationSummary.Fill(dsLocationSummary1);
CityChooser.DataSource = dsLocationSummary1;
CityChooser.DataTextField = "City";
CityChooser.DataValueField = "City";
CityChooser.DataBind();
for(int i = 0;i<CityChooser.Items.Count;i++)
{
while( (i+1) < CityChooser.Items.Count && CityChooser.Items.Value ==
CityChooser.Items[i+1].Value)
{
CityChooser.Items.RemoveAt(i+1);
}
}
dg_LocationSummary.DataBind();
}

I wasn't comparing the values before. But this doesn't feel like a very
good solution. Meaning what about when I'm doing this for 8000 cities?

-Brian
S. Justin Gengo said:
Brian,

You could filter your initial select statement by using the keyword
DISTINCT.

SELECT DISTINCT citynames FROM tblEntries

If you can't filter the records at the database request level then you can
always build a view in a similar fashion from those records.
Dim DataView As New DataView(dsLocationSummary1.Tables(0))

DataView.RowFilter = "DISTINCT citynames ASC"

CityChooser.DataSource = DataView;
CityChooser.DataTextField = "City";
CityChooser.DataValueField = "City";


Sincerely,

--
S. Justin Gengo, MCP
Web Developer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche
row,
but way
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top