| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 506 人关注过本帖
标题:[求助]一个关于数据绑定的问题!!~
只看楼主 加入收藏
维宜
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2006-5-30
收藏
 问题点数:0 回复次数:2 
[求助]一个关于数据绑定的问题!!~

在做系统时,我做了一个简单的窗体,具体如下图:

图片附件: 游客没有浏览图片的权限,请 登录注册

我想将combobox1与textbox1进行绑定.实现我在combobox中选择一个编号,对应的下面的textbox中就显示一个对应表中的姓名.而相关的记录都放于一张worker表中.
我利用读取器reader将数据读取到combobox中代码如下:
Dim i As Integer
Dim str As String
str = "select id from worker"
If conn.State = ConnectionState.Closed Then
conn.Open()
End If
comm.Connection = conn
comm.CommandType = CommandType.Text
comm.CommandText = str
da.SelectCommand = comm
da.Fill(ds, "worker")
'ComboBox1.Items.Add(ds.Tables("worker").Columns(0).ColumnMapping)
reader1 = comm.ExecuteReader()
If reader1.HasRows Then
While reader1.Read
ComboBox1.Items.Add(reader1(0))
End While
End If
reader1.Close()
然而,怎样去绑定选择的记录的姓名呢?
我这样做了一个比较死板的绑定.但是结果却只能显示一条!~换编号后就有错误了!~
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
If conn.State = ConnectionState.Closed Then
conn.Open()
End If
Dim str1 As String
str1 = "select * from worker"
comm.Connection = conn
comm.CommandType = CommandType.Text
comm.CommandText = str1
da.SelectCommand = comm
da.Fill(ds, "worker")
TextBox1.DataBindings.Add("text", ds, "worker.name")
End Sub
请高手指点!~谢谢!~

搜索更多相关主题的帖子: 绑定 数据 worker conn str 
2006-05-30 08:58
Viviwei
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:344
专家分:0
注 册:2006-4-11
收藏
得分:0 
一样的去用数据读取器哈,我按照你的这个方法修改一下,能成功!~
代码如下:
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
If conn.State = ConnectionState.Closed Then
conn.Open()
End If
Dim str1 As String
str1 = "select distinct name from worker where id = '" & ComboBox1.Text & "'"
comm.Connection = conn
comm.CommandType = CommandType.Text
comm.CommandText = str1
da.SelectCommand = comm
da.Fill(ds, "worker")
reader1 = comm.ExecuteReader()
If reader1.HasRows Then
While reader1.Read
TextBox1.Text = reader1.GetString(0)
End While
End If
If conn.State = ConnectionState.Open Then
conn.Close()
End If
End Sub

爱上思考,爱上编程,爱上.net!
2006-05-30 10:25
boxou
Rank: 1
等 级:新手上路
帖 子:135
专家分:0
注 册:2006-4-28
收藏
得分:0 

我也试过,可以哦,哈哈

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If SqlConn.State = ConnectionState.Closed Then
SqlConn.Open()
End If

sqlcmd = New SqlCommand("select ID from db_abc ", SqlConn)
sqlrd = sqlcmd.ExecuteReader()
If sqlrd.HasRows Then
While sqlrd.Read
ComboBox1.Items.Add(sqlrd(0))
End While
End If
sqlrd.Close()

End Sub

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

If SqlConn.State = ConnectionState.Closed Then
SqlConn.Open()
End If

sqlcmd = New SqlCommand("select Name from db_abc where ID = '" & ComboBox1.Text & "'", SqlConn)
sqlrd = sqlcmd.ExecuteReader()
If sqlrd.HasRows Then
While sqlrd.Read
TextBox1.Text = sqlrd.GetString(0)
End While
End If
sqlrd.Close()
If SqlConn.State = ConnectionState.Open Then
SqlConn.Close()
End If
End Sub


孔夫子说:博学、审问、慎思、明辩、力行[欢迎各位加入爱好者群:25620461]
2006-05-31 00:10
快速回复:[求助]一个关于数据绑定的问题!!~
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.033485 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved