在 vb.net 2008 中,如何在组合框中显示表列名称?
在 SQL 中,我有一个 Document 表,该表中有列,如 Doc_id、Size、Path。
组合框必须显示列名称,如
第一行是doc_id ,第二行是大小,最后一行是路径
Probably a little too late since I see this thing is about eight years old, but I just ran into this issue where I had to do exactly what the initial questioner was asking for. Since I resolved it, I will leave it here just incase another poor sap like us comes along looking for answers 'VB.net
Dim ds As DataSet = <define your dataset here>
Dim dt As DataTable = ds.Tables(<Your tablename here>)
For Each column As DataColumn In dt.Columns
combobox1.Items.Add(column.ColumnName)
Next
根据我们的讨论,我建议您不要将组合框直接与DataTalble
. 你可以通过循环所有的行来绑定它DataTable
。您使用以下片段作为参考:
Dim objDataTable As DataTable = ds.Tables("Document")
IF objDataTable <> NULL Then
For j As Integer = 0 To objDataTable.Rows.Count
Dim str As String = objDataTable.Rows(j)("Doc_id").ToString()
comboBox1.Items.Add(str)
str = objDataTable.Rows(j)("Size").ToString()
comboBox1.Items.Add(str)
str = objDataTable.Rows(j)("Path").ToString()
comboBox1.Items.Add(str)
Next
END IF
希望这对你有用;)