| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1086 人关注过本帖
标题:数据库与listbox的绑定问题
只看楼主 加入收藏
魔的天使之翼
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2007-9-21
收藏
 问题点数:0 回复次数:1 
数据库与listbox的绑定问题
'保存
    Private Sub MenuItem9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem9.Click
        Dim s As String, name As String
        If txttitle.Text = "" Then
            MsgBox("请输入日记标题", MsgBoxStyle.Exclamation Or MsgBoxStyle.OKOnly, "标题")
        Else
            name = txttitle.Text & ".txt"
            s = Application.StartupPath() + "\" & txttitle.Text & ".txt"
            RichTextBox1.SaveFile(s, RichTextBoxStreamType.PlainText)
            MsgBox("保存成功", MsgBoxStyle.OKOnly, "保存文件")

            Dim connstr, insertcmd As String
            Dim selectComd As String = "select * from riji"
            connstr = "provider=microsoft.jet.oledb.4.0;data source=rijititle.mdb"
            insertcmd = "insert into riji(title) values('" & getsqlstr(txttitle.Text) & " ')"

            Dim conn As OleDbConnection, myadapter As OleDbDataAdapter
            Dim mydataset As New DataSet
            Dim cmd As OleDbCommand
            conn = New OleDbConnection(connstr)
            conn.Open()
            cmd = New OleDbCommand(insertcmd, conn)
            cmd.ExecuteNonQuery()
            conn.Close()

            ListBox1.DataSource = mydataset.Tables("riji")
            Me.ListBox1.DisplayMember = "title"


        End If
    End Sub


'点击日志名字进入查看内容
    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        lbltitle.Text = ListBox1.SelectedItem
        Label1.Visible = False
        txttitle.Visible = False
        RichTextBox1.Visible = True
        ListBox1.Visible = False
        MenuItem1.Visible = True
        MenuItem5.Visible = True
        MenuItem8.Visible = True
        MenuItem9.Visible = True
        MenuItem10.Visible = True
        Button2.Visible = False
        btnselect.Visible = True
        lbltitle.Visible = True
        btndel.Visible = False
        btnsave.Visible = False
        RichTextBox1.LoadFile(Application.StartupPath + "\" + txttitle.Text + ".txt", RichTextBoxStreamType.PlainText)

    End Sub


我把listbox的displaymember 绑定数据库,点击listbox的一项,然后把这项的内容给label.text  结果
lbltitle.Text = ListBox1.SelectedItem  出错,说datarowview 不能强制转化为string 类型。要怎么循环读数据库,并把
一个字段的内容add到listbox 控件里面。
搜索更多相关主题的帖子: 数据库 绑定 listbox 
2007-12-11 17:00
fairy4
Rank: 4
等 级:贵宾
威 望:10
帖 子:738
专家分:267
注 册:2007-11-1
收藏
得分:0 
把记录读取到一个数据表中,根据表的记录条数
用一个for循环
把它添加到listbox里面me.listbox.items.add("数据")


单击listbox的一行
在listbox的click属性里面用
lbltitle.Text = ListBox1.SelectedItem.tostring()

下面是例子
 Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.ListBox1.Items.Add("aaa")
        Me.ListBox1.Items.Add("bbb")
        Me.ListBox1.Items.Add("ccc")
        Me.ListBox1.Items.Add("ddd")
        Me.ListBox1.Items.Add("eee")
        Me.ListBox1.Items.Add("fff")
        Me.ListBox1.Items.Add("hhh")
        Me.ListBox1.Items.Add("kkk")
    End Sub

    Private Sub ListBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.Click
        Me.lbltitle.Text = Me.ListBox1.SelectedItem.ToString()
    End Sub

一个人只有一个心脏,却有两个心房。一个住着快乐;一个住着悲伤。不要笑得太大声,不然会吵醒旁边的悲伤
2007-12-11 22:30
快速回复:数据库与listbox的绑定问题
数据加载中...
 
   



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

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