绑定组合框与数据表 [英] binding combobox with datatable
本文介绍了绑定组合框与数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
私有 Sub ComboBox1_SelectedIndexChanged(sender As System。 Object ,e As System.EventArgs)句柄 ComboBox1.SelectedIndexChanged
如果 ComboBox1.SelectedValue.ToString<> 然后
ComboBox1 .SelectedIndex = 0
结束 如果
结束 Sub
私有 Sub Form1_Load(sender As System。 Object ,e As System.EventArgs)句柄 MyBase .Load
con.ConnectionString = 数据源=。 \\ _SQLEXPRESS; AttachDbFilename = C:\ Users \SHAKEE \ AppData \ Local \Temporary Projects \ WindowsApplication1 \sponsorship.mdf; Integrated Security = True; Connect Ti meout = 30; User Instance = True
Dim ds 正如 新 DataSet
Dim adpt 作为 新 SqlDataAdapter
cmd.CommandType = CommandType.Text
cmd = 新 SqlCommand( SELECT name FROM child,con)
adpt。 SelectCommand = cmd
cmd.Connection = con
con.Open()
adpt.Fill(ds, child)
con.Close()
ComboBox1.DisplayMember = < span class =code-string> countryname
ComboBox1.DataSource = ds.Tables( 0 )。DefaultView
结束 Sub
结束 类
当我运行此代码时,我在comboboxlist中显示为 system.data.datarowview
我需要显示姓名。
如何清除此错误?
解决方案
您缺少DataTextField和DataValueField属性。正如他们的名字所示,第一个对应于要显示的文本,第二个对应于值一。
我已经解决了自己错误在
ComboBox1.DisplayMember = name
试试这个:
使用 ComboBox1
.DataSource = ds.tables( 0 )。defaultView
.DisplayMember = name
.Valuemember = name
结束 使用
Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
If ComboBox1.SelectedValue.ToString <> "" Then
ComboBox1.SelectedIndex = 0
End If
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
con.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\SHAKEE\AppData\Local\Temporary Projects\WindowsApplication1\sponsorship.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
Dim ds As New DataSet
Dim adpt As New SqlDataAdapter
cmd.CommandType = CommandType.Text
cmd = New SqlCommand("SELECT name FROM child", con)
adpt.SelectCommand = cmd
cmd.Connection = con
con.Open()
adpt.Fill(ds, "child")
con.Close()
ComboBox1.DisplayMember = "countryname"
ComboBox1.DataSource = ds.Tables(0).DefaultView
End Sub
End Class
When I run this code I got a display in comboboxlist as system.data.datarowview
I need to display name.
How can I clear this error?
解决方案
you are missing the DataTextField & DataValueField properties. As their names suggest, the first one correspond to the Text to show, and the second to the value one.
i have solved myself error is in
ComboBox1.DisplayMember = "name"
try this:
With ComboBox1 .DataSource=ds.tables(0).defaultView .DisplayMember="name" .Valuemember="name" End With
这篇关于绑定组合框与数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文