Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在VB.Net中将DGV表传递给报表查看器_Vb.net_Winforms_Datagridview_Pagination_Reportviewer - Fatal编程技术网

在VB.Net中将DGV表传递给报表查看器

在VB.Net中将DGV表传递给报表查看器,vb.net,winforms,datagridview,pagination,reportviewer,Vb.net,Winforms,Datagridview,Pagination,Reportviewer,我有一些用于DGV分页的代码,它工作正常。但最近我需要将DGV表传递给报表查看器。 我找到了一个执行此任务的代码片段,但有错误。。就像报表查看器只显示没有数据的表头或只显示某些列的数据一样,这是我将DGV表传递给报表查看器的代码 Form8.ReportViewer1.LocalReport.DataSources.Clear() Form8.ReportViewer1.LocalReport.ReportPath = Path.Combine(Application.StartupPath,

我有一些用于DGV分页的代码,它工作正常。但最近我需要将DGV表传递给报表查看器。 我找到了一个执行此任务的代码片段,但有错误。。就像报表查看器只显示没有数据的表头或只显示某些列的数据一样,这是我将DGV表传递给报表查看器的代码

 Form8.ReportViewer1.LocalReport.DataSources.Clear()
Form8.ReportViewer1.LocalReport.ReportPath = Path.Combine(Application.StartupPath, "Report1.rdlc")

Dim rds = New ReportDataSource("DataSet1", DataGridView1.DataSource) 
Form8.ReportViewer1.LocalReport.DataSources.Add(rds)

'Form8.ReportViewer1.Refresh()
'Form8.ReportViewer1.LocalReport.Refresh()
'Form8.ReportViewer1.RefreshReport()

 If Form8.ShowDialog() = DialogResult.OK Then
 Else
 End If
 Form8.Dispose()
我通过添加这些行,在转到报表查看器之前再次填充DGV,从而解决了这个问题

Dim con As New SqlConnection(cs) 
        Dim da As New SqlDataAdapter()
        Dim ds As New DataSet
        Dim cmd As New SqlCommand("select * from Emp")
        cmd.CommandType = CommandType.Text
        cmd.Connection = con
        da.SelectCommand = cmd
        da.Fill(ds, "Emp")
        DataGridView1.DataSource = ds.Tables("Emp")
DGV只有一页是可以的,但是如果我有多页,第一页只通过

我试图使用分页代码来解决此问题,但失败了。它从数据表“dtsource”克隆了一个数据表“dtTemp”,以便将DGV划分为多个页面。 这是我的代码,使分页到DGV它的工作方式像一个符咒

''' <summary>
''' The next lines are for making pagination to datagridview table, I will explain it briefly 
'''page size TextBox,It's for determine the number of the rows that i want it to appear to the user on the page,and i make the user change this number as he want from(1 row on the page to 100 rows only) the default is 25 rows
'''TextBox to DIsplay page number, It enables the user to show the whole number of pages and what is the current page,
'''(i.e) if DGV table results 200 rows and he write in page size TextBox 25 , He wiil have 8 pages.
'''And Four pictureboxes as (first,next,previous and last Buttons) To enable user to browse between pages,
'''You will see in every PictureBox the btnFill Button to fill the DGV again before loading the page by LoadPage(),And the Sub LoadPage() To load the cloned DataTable. 
'''The Function CheckFillButton() just for check if the btnFill is clicked or not in order to fill the DGV.
'''Really it is a nice code to make DGV divides into Pages.
'''Every Select Case statements are for avoiding errors ,I faced it during using this pretty snippet   
''' </summary>
Public dtSource As DataTable
Public PageCount As Integer
Public maxRec As Integer
Public pageSize As Integer
Public currentPage As Integer
Public recNo As Integer

    Sub LoadPage()
        Dim i As Integer
        Dim startRec As Integer
        Dim endRec As Integer
        Dim dtTemp As DataTable

        Select Case True
            Case DataGridView1.RowCount = Nothing

                Exit Sub
        End Select

        'Duplicate or clone the source table to create the temporary table.
        dtTemp = dtSource.Clone

        If currentPage = PageCount Then
            'MsgBox("currentPage = PageCount")
            endRec = maxRec
        Else
            endRec = pageSize * currentPage
        End If

        startRec = recNo

        'Copy the rows from the source table to fill the temporary table.
        For i = startRec To endRec - 1
            dtTemp.ImportRow(dtSource.Rows(i))
            recNo = recNo + 1
        Next

        DataGridView1.DataSource = dtTemp.DefaultView

        DisplayPageInfo()

    End Sub

    Private Function CheckFillButton() As Boolean

        'Check if the user clicks the "Fill Grid" button.
        If pageSize = 0 Then
            MessageBox.Show("PLS determine the size of pages", "Hint")
            CheckFillButton = False
        Else
            CheckFillButton = True
        End If

    End Function

    Sub DisplayPageInfo()
        txtDisplayPageNo.Text = "Page " & currentPage.ToString & "/ " & PageCount.ToString
    End Sub

    Private Sub PictureBox3_Click(sender As Object, e As EventArgs) Handles PictureBox3.Click
        'As button first
        Label5.Text = "first"
        If Not CheckFillButton() Then Return
        Select Case True
            Case Val(txtPageSize.Text) = 0 Or txtPageSize.Text = ""
                MsgBox("items cannot be zero", MsgBoxStyle.Exclamation, "Error")

                txtPageSize.Text = "25"
                Exit Sub
        End Select

        ' Check if you are already at the first page.
        If currentPage = 1 Then
            MessageBox.Show("You are at the First Page!")
            Return
        End If

        currentPage = 1
        recNo = 0

        LoadPage()

    End Sub

    Private Sub PictureBox4_Click(sender As Object, e As EventArgs) Handles PictureBox4.Click
        'As button next
        Label5.Text = "next"
        'If the user did not click the "Fill Grid" button then Return
        If Not CheckFillButton() Then Return
        Select Case True
            Case Val(txtPageSize.Text) = 0 Or txtPageSize.Text = ""
                MsgBox("items cannot be zero", MsgBoxStyle.Exclamation, "Error")

                txtPageSize.Text = "25"
                Exit Sub
        End Select

        'Check if the user clicked the "Fill Grid" button.
        If pageSize = 0 Then
            MessageBox.Show("Set the Page Size, and then click the button!")
            Return
        End If

        currentPage = currentPage + 1

        If currentPage > PageCount Then
            currentPage = PageCount

            'Check if you are already at the last page.
            If recNo = maxRec Then
                MessageBox.Show("You are at the Last Page!")

                Return
            End If
        End If

        Select Case True
            Case currentPage <= 1
                MsgBox("Your are in the same page " + vbCrLf +
                       "Try to divide it into pages",
                       MsgBoxStyle.Exclamation, "Error")
                Exit Sub
        End Select

        LoadPage()

    End Sub

    Private Sub PictureBox5_Click(sender As Object, e As EventArgs) Handles PictureBox5.Click
        'As button previous
        Label5.Text = "previous"
        If Not CheckFillButton() Then Return
        Select Case True
            Case Val(txtPageSize.Text) = 0 Or txtPageSize.Text = ""
                MsgBox("items cannot be zero", MsgBoxStyle.Exclamation, "Error")
                'txtPageSize.Focus()
                txtPageSize.Text = "25"
                Exit Sub
        End Select


        If currentPage = PageCount Then
            recNo = pageSize * (currentPage - 2)
        End If

        currentPage = currentPage - 1

        'Check if you are already at the first page.
        If currentPage < 1 Then
            MessageBox.Show("You are at the First Page!")

            currentPage = 1
            Return
        ElseIf currentPage = 1 Then
            'MessageBox.Show("current page = 1")
            recNo = pageSize
        Else
            recNo = pageSize * (currentPage - 1)
        End If

        'this select solve a problem to fill the grid
        Select Case True
            Case currentPage <= 1
                MsgBox("Your are in the page 1 " ,
                       MsgBoxStyle.Exclamation, "Error")
                btnFill_Click(sender, e)

                Exit Sub
        End Select

        LoadPage()

    End Sub

    Private Sub PictureBox2_Click(sender As Object, e As EventArgs) Handles PictureBox2.Click
        'As button last

        Label5.Text = "last"
        If Not CheckFillButton() Then Return
        Select Case True
            Case Val(txtPageSize.Text) = 0 Or txtPageSize.Text = ""
                MsgBox("items cannot be zero", MsgBoxStyle.Exclamation, "Error")

                txtPageSize.Text = "25"
                Exit Sub
        End Select

        ' Check if you are already at the last page.
        If recNo = maxRec Then
            MessageBox.Show("You are at the Last Page!", "Hint")
            Return
        End If

        currentPage = PageCount

        recNo = pageSize * (currentPage - 1)

        LoadPage()
        Movements(PictureBox2, 0.05)

    End Sub

    Private Sub txtPageSize_TextChanged(sender As Object, e As EventArgs) Handles txtPageSize.TextChanged
        If Val(txtPageSize.Text) > 100 Then
            Exit Sub
        End If
        Select Case True
            Case Val(txtPageSize.Text) = 0 Or txtPageSize.Text = ""
                MsgBox("Items cannot be Zero", MsgBoxStyle.Exclamation, "Error")
                txtPageSize.Text = "25"
                Exit Sub
        End Select
        btnFill_Click(sender, e)


    End Sub

    Private Sub btnFill_Click(sender As Object, e As EventArgs) Handles btnFill.Click

                Using conn As New SqlConnection(cs)
                    conn.Open()
                    Using cmd As New SqlCommand("select * from Emp")
                        'cmd.Parameters.Add("@a", SqlDbType.Int).Value = Val(TextBox5.Text)
                        cmd.CommandType = CommandType.Text
                        cmd.Connection = conn

                        'Set the DataAdapter's query.
                        Using ds As New DataSet(), da As New SqlDataAdapter(cmd)

                            ' Fill the DataSet.
                            da.Fill(ds, "Emp")
                            'DataGridView1.DataSource = ds.Tables("Emp")
                            ' Set the source table.
                            dtSource = ds.Tables("Emp")

                        End Using
                    End Using
                End Using

                'Set the start and max records. 
                pageSize = CInt(Val(txtPageSize.Text))
                maxRec = dtSource.Rows.Count

                PageCount = maxRec \ pageSize

                ' Adjust the page number if the last page contains a partial page.
                If (maxRec Mod pageSize) > 0 Then
                    PageCount = PageCount + 1
                End If

                'Initial seeings
                currentPage = 1
                recNo = 0

                ' Display the content of the current page.
                LoadPage()

   End Sub 
“”
''接下来的几行用于对datagridview表进行分页,我将简要说明
''页面大小文本框,用于确定我希望它在页面上显示给用户的行数,我让用户根据自己的需要更改此数字,从(页面上的1行更改为仅100行)默认为25行
''文本框显示页码,用户可以显示整个页数以及当前页面,
''”(即,如果DGV表格结果为200行,他在页面大小文本框25中写入,他将有8页。
''和四个图片框(第一个、下一个、上一个和最后一个按钮),使用户能够在页面之间浏览,
''您将在每个PictureBox中看到btnFill按钮,用于在通过LoadPage()加载页面之前再次填充DGV,以及子LoadPage()用于加载克隆的数据表。
''函数CheckFillButton()仅用于检查是否单击了btnFill以填充DGV。
''真的,这是一个很好的代码,可以将DGV划分为多个页面。
''每个Select Case语句都是为了避免错误,我在使用这个漂亮的片段时遇到了它
''' 
作为数据表的公共数据源
公共页面计数为整数
作为整数的公共maxRec
公共页面大小为整数
作为整数的公共currentPage
作为整数的公共recNo
子加载页()
作为整数的Dim i
Dim startRec作为整数
Dim endRec作为整数
Dim dtTemp As数据表
选择Case True
Case DataGridView1.RowCount=无
出口接头
结束选择
'复制或克隆源表以创建临时表。
dtTemp=dtSource.Clone
如果currentPage=PageCount,则
'MsgBox(“currentPage=PageCount”)
endRec=maxRec
其他的
endRec=页面大小*当前页面
如果结束
startRec=recNo
'复制源表中的行以填充临时表。
对于i=startRec至endRec-1
dtTemp.ImportRow(dtSource.Rows(i))
recNo=recNo+1
下一个
DataGridView1.DataSource=dtTemp.DefaultView
DisplayPageInfo()
端接头
作为布尔值的私有函数CheckFillButton()
'检查用户是否单击“填充网格”按钮。
如果pageSize=0,则
MessageBox.Show(“请确定页面大小”,“提示”)
CheckFillButton=False
其他的
CheckFillButton=True
如果结束
端函数
子显示页面信息()
txtDisplayPageNo.Text=“Page”¤tPage.ToString&“/”&PageCount.ToString
端接头
私有子PictureBox3\u单击(发送者作为对象,e作为事件参数)处理PictureBox3。单击
“首先是按钮
Label5.Text=“第一个”
如果没有选中FillButton(),则返回
选择Case True
Case Val(txtPageSize.Text)=0或txtPageSize.Text=“”
MsgBox(“项不能为零”,MsgBoxStyle.感叹号,“错误”)
txtPageSize.Text=“25”
出口接头
结束选择
'检查您是否已经在第一页。
如果currentPage=1,则
Show(“你在第一页!”)
返回
如果结束
当前页面=1
recNo=0
加载页()
端接头
私有子PictureBox4\u单击(发送者作为对象,e作为事件参数)处理PictureBox4。单击
“下一个按钮是什么
Label5.Text=“下一步”
'如果用户没有单击“填充网格”按钮,则返回
如果没有选中FillButton(),则返回
选择Case True
Case Val(txtPageSize.Text)=0或txtPageSize.Text=“”
MsgBox(“项不能为零”,MsgBoxStyle.感叹号,“错误”)
txtPageSize.Text=“25”
出口接头
结束选择
'检查用户是否单击了“填充网格”按钮。
如果pageSize=0,则
Show(“设置页面大小,然后单击按钮!”)
返回
如果结束
currentPage=currentPage+1
如果currentPage>PageCount,则
currentPage=页面计数
'检查您是否已经在最后一页。
如果recNo=maxRec,则
Show(“你在最后一页了!”)
返回
如果结束
如果结束
选择Case True

Case currentPage在搜索之后,执行此任务的好方法是创建一个datatable,然后向其中添加数据库列的列,然后在datagridview填充数据之后,在datagridview行中循环,以确保每个页面都传递到报表查看器,而不丢失任何数据。我将在下一篇评论Dim gdt中将其解释为新的DataTable gdt.Columns.Add(“ID”)gdt.Columns.Add(“EName”)gdt.Columns.Add(“Tel”),用于将I=0添加到DataGridView1.Rows.Count-1 gdt.Rows.Add(DataGridView1.Rows(I).Cells(0).Value,DataGridView1.Rows(I).Cells(1).Value,DataGridView1.Rows(i).Cells(2).Value)Next ReportViewer1.LocalReport.DataSources.Clear()ReportViewer1.LocalReport.ReportPath=“Report1.rdlc”