数据库与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 控件里面。