Monday, March 8, 2010

Assign datatable to combobox WPF

.XAMAL Code
ComboBox Height="23" HorizontalAlignment="Left" Margin="6,90,0,0" Name="cmbPlaceOfWork" VerticalAlignment="Top" Width="191"
(put xamal tags, < before ComboBox and /> after "191")

.XAMAL.vb Code
01-Imports System.ComponentModel

02-Private Sub FillPlaceOfWork()

Me.cmbPlaceOfWork.ItemsSource = (CType(DBRelated.GetDataTable("Select CME, FirstName As FullName From Personnel"), IListSource)).GetList()
Me.cmbPlaceOfWork.DisplayMemberPath = "FullName"
Me.cmbPlaceOfWork.SelectedValuePath = "CME"

End Sub

INFO: DBRelated.GetDataTable("Select CME, FirstName As FullName From Personnel")
The above function is from my class DBRelated and its just returning a datatable (i am using access as my backend db)

03-Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnSearch.Click
MessageBox.Show(Me.cmbPlaceOfWork.SelectedValue)
End Sub

Select some value and you can get the SelectedValue by the above property of combobox.

No comments: