谢谢版主!我查了,没有找到啊,能够写点具体的代码吗?不过这倒是多了几个问题,真头疼
这是我建立的存储过程:
Create proc Sb_S_BS_Book
@Sno Varchar(15)
AS
Select Book.ID,Book.Name,BS.BeginDate,BS.RemainDays
From Students,BS,Book
Where Students.SID=BS.SID and BS.ID=Book.ID and
Students.Sno=@Sno /*三张表,一张Book,一张Students,还有一张为BS[其中Book表和Students表的主键作为BS的外键]
在应用程序端,能够成功将表的内容显示在DataGrid中显示:
代码: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If mycon.State = ConnectionState.Closed Then
mycon.Close()
End If
Dim myset As New DataSet
Dim sqlstr As String = "Exec Sb_S_BS_Book @Sno='" & TextBox1.Text & "'"
Dim myda As New SqlDataAdapter(sqlstr, mycon)
myda.Fill(myset, "information")
DataGrid1.DataSource = myset.Tables("information")
End Sub/*成功执行*/
然而显示在ListView中却出现问题了,代码如下:
Private Sub FillDataToListView1()
Try
If mycon.State = ConnectionState.Closed Then
mycon.Open()
End If
Dim sqlstr As String = "Exec Sb_S_BS_Book @Sno='" & TextBox1.Text & "'"
Dim myda As New SqlDataAdapter(sqlstr, mycon)
Dim myset As New DataSet
myda.Fill(myset, "information")
Dim i As Integer
Dim item As ListViewItem
Dim Subitem2 As ListViewItem.ListViewSubItem
Dim Subitem3 As ListViewItem.ListViewSubItem
Dim Subitem4 As ListViewItem.ListViewSubItem
For i = 0 To myset.Tables("information").Rows.Count - 1
With myset.Tables("information").Rows(i)
item = New ListViewItem
Subitem2 = New ListViewItem.ListViewSubItem
Subitem3 = New ListViewItem.ListViewSubItem
Subitem4 = New ListViewItem.ListViewSubItem
item = .Item(0) & ""
Subitem2 = .Item(1) & ""
Subitem3 = .Item(2) & ""
Subitem4 = .Item(3) & ""
item.SubItems.Add(Subitem2)
item.SubItems.Add(Subitem3)
item.SubItems.Add(Subitem4)
ListView1.Items.Add(item)
End With
Next
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
报错说:格式转换不正确
好!我又稍微改了一下:
将 item = .Item(0) & ""
Subitem2 = .Item(1) & ""
Subitem3 = .Item(2) & ""
Subitem4 = .Item(3) & ""
改为:
item = .Item("Book.ID") & ""
Subitem2 = .Item("Book.Name") & ""
Subitem3 = .Item("BeginDate") & ""
Subitem4 = .Item("BS.RemainDays") & "" 其余不变,这里变了一下
又说Book.ID不属于information
问题总结:
<1>在将数据库中的一张表和几张表的内容通过DataSet显示在DataGrid中没有问题,将数据库中的一张表显示在ListView中没有问题,然而将数据库中的几张表的内容显示在ListView中却出现问题,所以,我得出的结论是:将数据库中的几张表的内容显示在ListView中是不能办到的,我不知道我的结论是否正确,如果错误,望大家能够给我指出其中的错误,和应该注意的地方
<2>在ListView中我们能够通过Columns属性来提前设置其列的名称,在DataGrid中也能办到这一点吗?提前设置列的 名称
而不是在运行后自动显示
<3>还是ListView的问题,怎么把ListView中指定列的数据赋给指定的变量,有点代码更好,谢谢大家,看起来肯定很烦哈!呵呵