ComboBox not filled with DataTable - Why?

VBobCat
12-15-2011, 05:32 AM
I used this code:


Friend Sub PopulateCB(ByRef mycontrol As ComboBox, _
ByVal expressionSQL As String)
' DMBD is my data-handling class:
Using DataTableOrigem As DataTable = DMBD.Read_a_Table(expressionSQL)
' this MsgBox told me my Table is correctly filled:
MsgBox(DataTableOrigem.Rows(0).Item(0).ToString)
' this MsgBox told me my control was correctly passed:
MsgBox(mycontrol.Name)
mycontrol.DataSource = DataTableOrigem
mycontrol.ValueMember = DataTableOrigem.Columns(0).ColumnName
mycontrol.DisplayMember = DataTableOrigem.Columns(1).ColumnName
End Using
End Sub


Nevertheless, mycontrol remains empty after this.

Could someone help me figure out why?

Thank you very much!

VBobCat
12-15-2011, 11:56 AM
My own mistake. I just have been told about it. Using statement disposes the used resource at end of block, so I have disposed the DataTable and, with it, all ComboBox data, which was bound and referred to that DataTable.

I've already tested the fixed code. It made all ok and it is, also, far more elegant:


Friend Sub PopulateCB(ByRef mycontrol As ComboBox, ByVal expressionSQL As String)
mycontrol.DataSource = DMBD.Read_a_Table(expressionSQL)
mycontrol.ValueMember = DirectCast(mycontrol.DataSource, DataTable).Columns(0).ColumnName
mycontrol.DisplayMember = DirectCast(mycontrol.DataSource, DataTable).Columns(1).ColumnName
End Sub


Thanks to all of you that spent some thinking over the question!

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum