vb.net - 将 DGV 表传递给 VB.Net 中的报表查看器
问题描述
我有一些用于分页 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 没问题,但如果我有不止一页,第一页只能通过
我尝试使用我的分页代码来解决问题,但我失败了..它有一个从 DataTable“dtsource”克隆的 Datatable“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
问题是如何将 DGV 的每一页传递给报告查看器,(即)当我在 DGV 页面之间浏览时,我怎样才能只传递我已经在上面的页面(它只包含 25 行作为页面大小说)。上面的代码再次将整个表加载到 DGV 然后它传递整行。希望我在这里找到帮助
解决方案
推荐阅读
- android - TensorImage 无法加载位图
- c# - C#不理解委托的协变和逆变
- android - Firebase 分析显示未知的发布版本和屏幕名称。这怎么可能
- django - 使用应用程序密码发送电子邮件 - 不接受用户名和密码?
- graphql - Apollo graphQL 订阅使用哪个包
- python-3.x - Python:在字符串上使用 .format() 时使用带有 lambda 函数的 dict
- c# - HttpWebRequest 适用于 .NetFramework 但不适用于 .NetCore
- .net - 在扩展递归方法 (F#) 中从 FOR ... IN 返回?
- javascript - 如何将 eventEventListner 添加到已创建
- javascript - 异步等待分配内部的函数