Combobox数据源分配给Datatable

时间:2015-06-04 11:38:20

标签: vb.net combobox casting datatable

如何在Comb.net中将Combobox的数据源转换为Datatable? 我知道如何assign datatable to combobox。在这种情况下,我需要采取相反的方式。

像:

  

dim dt as DataTable = combobox1.DataSource

这可能吗?

1 个答案:

答案 0 :(得分:1)

我会使用TryCast来完成工作。

Dim dt As DataTable = TryCast(combobox1.DataSource, DataTable)

If dt Is Nothing Then
    ' It didn't work because it's not a datatable
    ' Editors: I specifically did not reduce this expression
    '          to emphasize the need to check for 'Nothing'
    '          and handle it appropriately.
Else
    ' Yay it's a datatable
End If