Solved

Visual Studio 2008 Populate a Combo Box with Distinct Values

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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

718 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