Internet is growing Every Seconds :: Industry is growing Every Year

Wednesday, June 23, 2004

Dot net Public Function Fill_Combo

Function Fill_Combo :

This method is used to fill the given Combobox with the data from the specified table.
It accepts 2 arguments, namely control and Dt
Control – object of the Combobox class.
Dt – object of the DataTable.
Following code snippet is the key lines.
control.DataSource = dt

control.DisplayMember = dt.Columns(1).ToString

control.ValueMember = dt.Columns(0).ToString

First line assigns ‘dt’ as the Datasource property for the Combobox.
Second line assign the string data to be displayed in the combobox as DsiplayMember property for the combobox.
Third line assign the data value to be gotten when the item is selected in the combobox. The property is ValueMember.

Public Function Fill_Combo(ByRef control As ComboBox, ByRef dt As DataTable)
Dim Not_Dt As DataTable
Try
If dt.Rows.Count > 0 Then
control.DataSource = dt
control.DisplayMember = dt.Columns(1).ToString
control.ValueMember = dt.Columns(0).ToString
Else
Not_Dt = tbl_Not_Available()
control.DataSource = Not_Dt
control.DisplayMember = Not_Dt.Columns(1).ToString
control.ValueMember = dt.Columns(0).ToString
'control.Items.Add("No Data Available")
End If
Catch
End Try
End Function