Solved

Visual Studio 2008 Populate a Combo Box with Distinct Values

Posted on 2013-12-20
2
1,903 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

734 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