在Crystal Report中显示DataGrid数据时出现问题

问题描述:

我正在将数据网格中的数据显示到Crystal报表中.
为此,我使用了数据集控件,在其中添加了数据表&数据集控件中的列数(与datagrid中的列数相同).
然后,我尝试使用以下给定的代码填充该数据集,但出现错误对象引用未设置为对象的实例".我对水晶报告一无所知.
我的代码是

I am displaying the data from datagrid into crystal report.
For that I have used a data set control,added data table in it & the number of columns in the data set control(same as the number =of columns in datagrid).
Then I am trying to populate that dataset using the below given code but it gives me error "Object reference not set to an instance of an object." I have very little knowledge of crystal report.
My code is

Dim ds As DsRep = New DsRep()
       Dim total As Int16 = frmServerViewReports.DataGridView1.Rows.Count
       Dim i As Int16
       For i = 0 To (total - 1)
           ds.Tables(0).Rows.Add(frmServerViewReports.DataGridView1(0, i).Value.ToString, frmServerViewReports.DataGridView1(1, i).Value.ToString, frmServerViewReports.DataGridView1(2, i).Value.ToString, frmServerViewReports.DataGridView1(3, i).Value.ToString, frmServerViewReports.DataGridView1(4, i).Value.ToString)
       Next



如果数据集控件frmServerViewReport是放置数据网格的表单的名称,则DsRep是名称.



Here DsRep is the name if the dataset control,frmServerViewReport is the name of the form on which the datagrid is placed

Dim ds As DsRep = New DsRep()
       Dim total As Int16 = frmServerViewReports.DataGridView1.Rows.Count
       Dim i As Int16
       Do Until i = (total - 1)
           Dim a() As String = {frmServerViewReports.DataGridView1(0, i).Value.ToString, frmServerViewReports.DataGridView1(1, i).Value.ToString, frmServerViewReports.DataGridView1(2, i).Value.ToString, frmServerViewReports.DataGridView1(3, i).Value.ToString, frmServerViewReports.DataGridView1(4, i).Value.ToString}
           ds.Tables(0).Rows.Add(a)
           i = i + 1
       Loop
       Dim obj As New crRep
       obj.SetDataSource(ds)
       CrystalReportViewer1.ReportSource = obj



用它来获得正确的输出



Use this to get the proper output