Solved

Visual Studio 2008 Populate a Combo Box with Distinct Values

Posted on 2013-12-20
2
1,878 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 63

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.net Filesystem watcher not working 5 37
SQL Syntax 6 29
SQL question - help with insert for missing info 5 19
SQL syntax question 6 39
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

756 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