Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combo Box Values 3

Status
Not open for further replies.

aharris2693

Programmer
Jul 15, 2003
45
0
0
US
I think this is a relatively simple question, but being new to .Net, I don't know the answer to it. I have built a combo box that is filled through a datatable. All of this works, but what I am trying to do is add "PersonID", another field in the datatable as the value of each entry. I tried value member, but that just holds the last one in the set. Below is part of my code, any help would be much appreciated.


Dim MyDataTable As New System.Data.DataTable("EmployeeInfo")

objDA.Fill(MyDataTable)
Dim myDataRow As DataRow
For Each myDataRow In MyDataTable.Rows
strName = Convert.ToString(myDataRow("Expr1"))
intID = Convert.ToDouble(myDataRow("PersonID"))

cmbName.DisplayMember = (strName)


Next
 

The following sample explains how you can bind a ComboBox to a datasource and set ValueMember & DisplayMember properties. Tweak the code as per your requirement

Dim selectString As String
Dim objDA As OleDbDataAdapter
Dim objDS As New DataSet()

Try
'Initialize SQL string.
selectString = "SELECT Field1, Field2 FROM TableName"

'Fill the DataSet with data, using OleDbDataAdapter
objDA = New OleDbDataAdapter(selectString, connectionString)
objDA.Fill(objDS, "TableName")

'Set DataSource, ValueMember and DisplayMember property for Combo Box.
ComboBox1.DataSource = objDS.Tables("TableName")
ComboBox1.ValueMember = objDS.Tables("TableName").Columns("Field1").ToString
ComboBox1.DisplayMember = objDS.Tables("TableName").Columns("Field2").ToString


Catch Excep As System.Exception
MessageBox.Show(Excep.Message, "Prospect Management System (popState)", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try


Email: pankajmsm@yahoo.com
 

The following sample explains how you can bind a ComboBox to a datasource and set ValueMember & DisplayMember properties. Tweak the code as per your requirement

Dim selectString As String
Dim objDA As OleDbDataAdapter
Dim objDS As New DataSet()

Try
'Initialize SQL string.
selectString = "SELECT Field1, Field2 FROM TableName"

'Fill the DataSet with data, using OleDbDataAdapter
objDA = New OleDbDataAdapter(selectString, connectionString)
objDA.Fill(objDS, "TableName")

'Set DataSource, ValueMember and DisplayMember property for Combo Box.
ComboBox1.DataSource = objDS.Tables("TableName")
ComboBox1.ValueMember = objDS.Tables("TableName").Columns("Field1").ToString
ComboBox1.DisplayMember = objDS.Tables("TableName").Columns("Field2").ToString


Catch Excep As System.Exception
MessageBox.Show(Excep.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try


Email: pankajmsm@yahoo.com
 
Thanks a lot PankajBanga. Just what I was looking for.
 
THANK YOU, THANK YOU, THANK YOU PANKAJBANJA!!!!!!

I am a newbie to .net and I have been stumbling my way through it. I had all sorts of tools/functions to do everything I need to in VB 6.0 ([banghead] [curse] [hairpull]), but they are basically useless now.

Thanks Again!


For You: [medal] [thumbsup2]

[spidey] [americanflag] [unclesam]
 
After you have set the DataSource, then I believe you can shorten the above code as follows:
Code:
'Set DataSource, ValueMember and DisplayMember property for Combo Box.
ComboBox1.DataSource = objDS.Tables("TableName")
ComboBox1.ValueMember = "Field1" 
ComboBox1.DisplayMember = "Field2"


Have a great day!

j2consulting@yahoo.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top