Solved

Visual Studio 2008 Populate a Combo Box with Distinct Values

Posted on 2013-12-20
2
1,829 Views
Last Modified: 2013-12-21
I have a Visual Studio 2008 Form  

i have called frm_1

On the Form a have a Combo Box
i have called cbo_1

i have a dataset

i have called dataset1

 link to a Table

i have called the table tbl_1

the Table have 4 Collums : ID_Name / Name / City / State /

I want use the Combo Box to pass a parameter to a Query  that i want to develop

The query will  filter by State ( but this question do not involve the Query , it related on how populate the Combo Box )
So what is the code to fill the cbo_1 with the States listed into the tbl_1 ( linked to the dataset1???

so the cbo_1 should list the state ( as unique value ) i mean i want be able to
Have unique state list on cbo_1
0
Comment
Question by:sanlorenzo
2 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 39732650
Hi sanlorenzo;

I am assuming that you have the latest SP for Visual Studio 2008. You can use Linq to Object to create a list to feed the ComboBox as follows.

Dim stateList = (From t in tbl_1.AsEnumerable()
                 Select t.Field(Of String)("States")).Distinct().ToList()
			           
cbo_1.DataSource = stateList

Open in new window

0
 
LVL 13

Accepted Solution

by:
Jesus Rodriguez earned 500 total points
ID: 39733042
'This is because more than one city can be on the same state and you're getting the max(ID)
'for the state regarding any city if not you have to create a table for the states by itself
'with ID and state_name

Dim Qry as String="SELECT MAX(ID_NAME) AS ID ,[STATE] FROM TABLE_NAME GROUP BY [STATE]"
DIM T AS NEW DATATABLE
DIM ADT AS New SqlDataAdapter(QRY,Your_ConnectionString)
T.CLEAR
ADT.FILL(T)
Dim RW as DataRow=t.NewRow
 RW("ID")=0
 RW("STATE")="Select State"
 T.Add(Rw)
With ComBobox1
 .DataValueMember="ID"
 .DisplayMember="STATE"
 .DataSource=T
 .DataBind()
 .SelecteItem=0
End With

Or the Qery for the Combobox Will Be
SELECT MAX(ID_NAME) AS ID ,[STATE] FROM TABLE_NAME GROUP BY [STATE]
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question