我正在尝试将datagridview数据提供给Crystal报表,但我收到此错误消息“输入数组比此表中的列数长。”
任何想法如何解决我的错误?
Sub PrintToCR()
Dim dt As New DataTable
For Each dr As DataGridViewRow In Me.DataGridView1.Rows
dt.Rows.Add(dr.Cells("ProductID").Value, dr.Cells("BrandName").Value, dr.Cells("GenericName").Value, _
dr.Cells("ExpirationDate").Value, dr.Cells("Price").Value, _
dr.Cells("Unit").Value, dr.Cells("QuantityOnHand").Value) '<<<<< Error is HERE.
Next
'
Dim rptDoc As CrystalDecisions.CrystalReports.Engine.ReportDocument
rptDoc = New CrystalReport1
rptDoc.SetDataSource(dt)
'
CrystalReportViewer.CrystalReportViewer1.ReportSource = rptDoc
CrystalReportViewer.ShowDialog()
CrystalReportViewer.Dispose()
End Sub
这是我的数据库交易
Public Class data
Dim connString As New connection
Dim con As New OleDb.OleDbConnection
Dim cmd As New OleDb.OleDbCommand
Dim da As New OleDb.OleDbDataAdapter
Public Function List(ByVal sql As String) As DataSet
con.ConnectionString = connString.connectionString
con.Open()
da = New OleDb.OleDbDataAdapter(sql, con)
Dim ds As New DataSet
da.Fill(ds, "data")
con.Close()
List = ds
End Function
Public Sub ExecuteSql(ByVal sql As String)
con.ConnectionString = connString.connectionString
con.Open()
cmd.CommandText = sql
cmd.CommandType = CommandType.Text
cmd.Connection = con
cmd.ExecuteNonQuery()
con.Close()
End Sub
结束班
顺便说一下,我的Datagridview是通过Datasource连接的
感谢
答案 0 :(得分:0)
试试这个..
Dim dt As New DataTable
dt.Columns.Add("ProductId")
dt.Columns.Add("BrandName")
dt.Columns.Add("GenericName")
dt.Columns.Add("ExpirationDate")
dt.Columns.Add("Price")
dt.Columns.Add("Unit")
dt.Columns.Add("QuantityOnHand")
For Each dr As DataGridViewRow In Me.DataGridView1.Rows()
dt.Rows.Add(dr.Cells("ProductID").Value, dr.Cells("BrandName").Value, dr.Cells("GenericName").Value, _
dr.Cells("ExpirationDate").Value, dr.Cells("Price").Value, _
dr.Cells("Unit").Value, dr.Cells("QuantityOnHand").Value)
Next
'
Dim rptDoc As CrystalDecisions.CrystalReports.Engine.ReportDocument
rptDoc = New CrystalReport1
rptDoc.SetDataSource(dt)
CrystalReportViewer.CrystalReportViewer1.ReportSource = rptDoc
CrystalReportViewer.ShowDialog()
CrystalReportViewer.Dispose()