eZine4i.com - Free Articles Directory From Search4i Network
   
   
 
 

ARTICLE CATEGORIES

SEARCH4i NETWORK

Follow Me on Twiter

ADVERTISEMENTS

 

TOP 5 AUTHORS

Alan Smith 533
Julia Bennet 370
Angelo Everton 369
Ashish Pandey 333
Kaye Z. Marks 291

Bind an ADO.NET DataTable to a Combobox in VB.NET

addthis
     
Author:

Chetas

Category: HomearrowProgramming
Summary:

Bind an ADO.NET DataSet or DataTable to a Combobox in VB.NET using the ComboBox's DisplayMember and ValueMember properties to display one value but use a different value.

Article:
You can bind a DataTable to a Combobox to display one value in the ComboBox's list while using another value to identify rows. VB6 provided the ItemData property for this purpose. .NET did away with the ItemData property but allows you to bind almost any type of data object to a ComboBox.

This code shows how to bind a DataTable to a ComboBox and display column 1 of the DataTable (the "Description" column) in the ComboBox and use column 2 ("Code") to select ComboBox items.

NOTE: a bug exists in the .NET Framework V1.1 when the ComboBox is used on a Tab Control. The bug causes the ComboBox to select its first item when the tab control's selected index changes. Also, the ComboBox cannot be cleared unless its SelectedIndex property is set to -1 twice.


Retrieve the data into a DataSet:

Dim strSQL As String = "Select Code,Description From MyTable"
Dim Connection As New OleDbConnection("PROVIDER=....")
Dim DA As New OleDbDataAdapter(strSQL, Connection)
Dim DS As New DataSet

DA.Fill(DS, "Codes")

Create and populate the DataTable to bind to the ComboBox:

Dim dt As New DataTable
dt.Columns.Add("Description", GetType(System.String))
dt.Columns.Add("Code", GetType(System.String))
'
' Populate the DataTable to bind to the Combobox.
'
Dim drDSRow As DataRow
Dim drNewRow As DataRow

For Each drDSRow In DS.Tables("Codes").Rows()
drNewRow = dt.NewRow()
drNewRow("Description") = drDSRow("Description")
drNewRow("Code") = drDSRow("Code")
dt.Rows.Add(drNewRow)
Next

Bind the DataTable to the ComboBox by setting the Combobox's DataSource property to the DataTable. To display the "Description" column in the Combobox's list, set the Combobox's DisplayMember property to the name of column. Likewise, to use the "Code" column as the value of an item in the Combobox set the ValueMember property.

ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList

With ComboBox1
.DataSource = dt
.DisplayMember = "Description"
.ValueMember = "Code"
.SelectedIndex = 0
End With

To select an item based on the "Code" or ValueMember property:

Dim aIndex As Integer

With ComboBox1
For aIndex = 0 To .Items.Count - 1
If CType(.Items(aIndex)(1), String).Trim = TextBox2.Text.Trim Then
.SelectedIndex = aIndex
Exit For
End If
Next

If aIndex >= .Items.Count Then .SelectedIndex = -1
End With
Source: Free Articles from ezine4i.com
About Author: emailverified

Rate It:
     

TOP

RELATED ARTICLES

bullet Important Facts About iPhone Application Development
By:Shital Thakkar Category:Programming
bullet PHP Web Development Company and its Advantages - Rising in Demand of PHP Developers
By:Dave Sprint Category:Programming
bullet Open Source Development - Make your website user- friendly and SEO-friendly
By:Dave Sprint Category:Programming
bullet iOS Development: An Introduction to iPhone App Development
By:Dave Sprint Category:Programming
bullet PhpStorm: Smart & fast PHP IDE
By:Alexander Morozov Category:Programming
bullet Magento Development - Get perfect Ecommerce Solution with Magento
By:Dave Sprint Category:Programming
bullet Hire Joomla Developer - Customize your business needs with Joomla Development
By:Dave Sprint Category:Programming
bullet Google Calendar Component (Delphi Internet Components)
By:Sergey Shirokov Category:Programming
bullet Source code analysis for all your Databases, Crystal Reports, data and software program files
By:Ken Gnazdowsky Category:Programming
bullet android development,android developers
By:Godwin Josh Category:Programming