none
Passing List View items to a report viewer in windows form using vb.net RRS feed

  • Question

  • Hi Everyone

    I have a list view that contained more than one row i want to pass it to a report viewer but i face a problem 

    if i have one row it appears in report viewer (repeated four times) and if i have more than one row the last row only appears 

    (repeated four times) like this picture 

    Here is My code , in case of using for loop or not ,i get the same result.....

      'Print()
            Dim con As New OleDbConnection(cs)
            Dim da As New OleDbDataAdapter()
            Dim ds As New DataSet
            Dim cmd As New OleDbCommand("SELECT * FROM BillSold")
            cmd.CommandType = CommandType.Text
            cmd.Connection = con
            da.SelectCommand = cmd
            'ds.Clear()
            da.Fill(ds, "DataSet1")
    
            Dim rds As New ReportDataSource("DataSet1", ds.Tables(0))
    
            SearchForm15.ReportViewer1.LocalReport.ReportPath = Path.Combine(Application.StartupPath, "E:\KMAStock\KMAStock\Report1.rdlc")
            SearchForm15.ReportViewer1.LocalReport.DataSources.Clear()
            SearchForm15.ReportViewer1.LocalReport.DataSources.Add(rds)
    
            Dim invoiceno As New ReportParameter("InvoiceNoParam", Me.txtInvoiceNo.Text)
            Dim solddate As New ReportParameter("SoldDateParam", Me.dtpTransactionDate.Text)
            Dim customername As New ReportParameter("CustomerNameParam", Me.cmbcustomername.Text)
    
    
            'Dim i As Integer
            'For Each ii In ListView1.Items(0).SubItems
    
            For i = 0 To ListView1.Items.Count - 1
                If ListView1.Items.Count <> 0 Then
    
                    'For i = 0 To ListView1.Items.Count - 1
                    With ListView1.Items.Item(i)
                        Dim productcode As New ReportParameter("ProductCodeParam", Val(.SubItems(1).Text))
                        Dim productparam As New ReportParameter("ProductParam", (.SubItems(2).Text))
                        Dim sprice As New ReportParameter("SellPriceParam", Val(.SubItems(5).Text))
                        Dim soldcartons As New ReportParameter("SoldCartonsParam", Val(.SubItems(6).Text))
                        Dim soldpackets As New ReportParameter("SoldPacketsParam", Val(.SubItems(7).Text))
                        Dim totalamount As New ReportParameter("TotalAmountParam", Val(.SubItems(8).Text))
                       
                        SearchForm15.ReportViewer1.LocalReport.SetParameters(New ReportParameter() {productcode})
                        SearchForm15.ReportViewer1.LocalReport.SetParameters(productparam)
                        SearchForm15.ReportViewer1.LocalReport.SetParameters(sprice)
                        SearchForm15.ReportViewer1.LocalReport.SetParameters(soldpackets)
                        SearchForm15.ReportViewer1.LocalReport.SetParameters(soldcartons)
                        SearchForm15.ReportViewer1.LocalReport.SetParameters(totalamount)
                    End With
                End If
            Next
    
            'Next
            'For i = 0 To ListView1.Items.Count - 1
            '    Dim productcode As New ReportParameter("ProductCodeParam", Val(Me.ListView1.Items(i).SubItems(1).Text))
            '    Dim productparam As New ReportParameter("ProductParam", (Me.ListView1.Items(i).SubItems(2).Text))
            '    Dim sprice As New ReportParameter("SellPriceParam", Val(Me.ListView1.Items(i).SubItems(5).Text))
            '    Dim soldcartons As New ReportParameter("SoldCartonsParam", Val(Me.ListView1.Items(i).SubItems(6).Text))
            '    Dim soldpackets As New ReportParameter("SoldPacketsParam", Val(Me.ListView1.Items(i).SubItems(7).Text))
            '    Dim totalamount As New ReportParameter("TotalAmountParam", Val(Me.ListView1.Items(i).SubItems(8).Text))
            '    'If cbxPatName.Text = String.Empty Then
            '    '    MsgBox("ادخل اسم العميل", MsgBoxStyle.Information, "اسم العميل مطلوب")
            '    '    Exit Sub
            '    'ElseIf cbxPatName.Text <> String.Empty Then
            '    '    TabControl1.SelectedTab = Me.TabPage6
    
            '    SearchForm15.ReportViewer1.LocalReport.SetParameters(productcode)
            '    SearchForm15.ReportViewer1.LocalReport.SetParameters(productparam)
            '    SearchForm15.ReportViewer1.LocalReport.SetParameters(sprice)
            '    SearchForm15.ReportViewer1.LocalReport.SetParameters(soldpackets)
            '    SearchForm15.ReportViewer1.LocalReport.SetParameters(soldcartons)
            '    SearchForm15.ReportViewer1.LocalReport.SetParameters(totalamount)
            '    'Exit For
            'Next
    
            'For i = 0 To ListView1.Items.Count - 1
            '    Dim productcode As New ReportParameter("ProductCodeParam", Val(Me.ListView1.Items(i).SubItems(1).Text))
            '    Dim productparam As New ReportParameter("ProductParam", (Me.ListView1.Items(i).SubItems(2).Text))
            '    Dim sprice As New ReportParameter("SellPriceParam", Val(Me.ListView1.Items(i).SubItems(5).Text))
            '    Dim soldcartons As New ReportParameter("SoldCartonsParam", Val(Me.ListView1.Items(i).SubItems(6).Text))
            '    Dim soldpackets As New ReportParameter("SoldPacketsParam", Val(Me.ListView1.Items(i).SubItems(7).Text))
            '    Dim totalamount As New ReportParameter("TotalAmountParam", Val(Me.ListView1.Items(i).SubItems(8).Text))
            '    'If cbxPatName.Text = String.Empty Then
            '    '    MsgBox("ادخل اسم العميل", MsgBoxStyle.Information, "اسم العميل مطلوب")
            '    '    Exit Sub
            '    'ElseIf cbxPatName.Text <> String.Empty Then
            '    '    TabControl1.SelectedTab = Me.TabPage6
            '    SearchForm15.ReportViewer1.LocalReport.SetParameters(productcode)
            '    SearchForm15.ReportViewer1.LocalReport.SetParameters(productparam)
            '    SearchForm15.ReportViewer1.LocalReport.SetParameters(sprice)
            '    SearchForm15.ReportViewer1.LocalReport.SetParameters(soldpackets)
            '    SearchForm15.ReportViewer1.LocalReport.SetParameters(soldcartons)
            '    SearchForm15.ReportViewer1.LocalReport.SetParameters(totalamount)
            'Next
            'End If
            SearchForm15.ReportViewer1.LocalReport.SetParameters(invoiceno)
            SearchForm15.ReportViewer1.LocalReport.SetParameters(solddate)
            SearchForm15.ReportViewer1.LocalReport.SetParameters(customername)
    
            'SearchForm15.ReportViewer1.SaveReport()
            SearchForm15.ReportViewer1.Refresh()
            SearchForm15.ReportViewer1.RefreshReport()
            SearchForm15.ReportViewer1.LocalReport.Refresh()
    
    
            SearchForm15.TabControl1.SelectedTab = SearchForm15.TabPage4
            SearchForm15.ShowDialog()

    Any suggestions will be appreciated 

    Thanks in advance..........

     

    Regards From Amr_Aly

    Tuesday, November 13, 2018 1:11 PM

Answers

  • Hello,

    Every time Listview will have fixed no. of Rows in your case ? If yes then you can create parameters and use that ,

    If not then in that case I will suggest just create a dummy table in your database with no. of columns equal to no. of columns in listview.

    Then convert listview to datatable and then pass datatable content to rdlc report table like below

    Hope below code helps you

    CREATE TABLE [dbo].[DummyTable](
    	[ProductCode] [varchar](100) NULL,
    	[ProductName] [varchar](100) NULL
    ) ON [PRIMARY]
    Imports Microsoft.Reporting.WinForms
    
    Public Class Form17
    
        Private Sub Form17_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Me.ReportViewer1.RefreshReport()
            'Fill Listview
            Try
                Dim productcode As List(Of String) = {"P01", "P02", "P03", "P04", "P05"}.ToList()
                Dim productname As List(Of String) = {"PName1", "PName2", "PName3", "PName4", "PName5"}.ToList()
                Dim str(2) As String
                For i = 0 To 4
                    Dim itm As ListViewItem
                    str(0) = productcode(i)
                    str(1) = productname(i)
                    itm = New ListViewItem(str)
                    ListView1.Items.Insert(0, itm)
                Next
            Catch ex As Exception
                Dim errormsg As String = String.Empty
                errormsg = ex.ToString()
            End Try
        End Sub
        'generate report from Listview Items
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Try
                'Convert Listview to Datatable
                Dim dt As New DataTable
                dt.Columns.Add("ProductCode")
                dt.Columns.Add("ProductName")
                For i = 0 To ListView1.Items.Count - 1
                    dt.Rows.Add(ListView1.Items(i).SubItems(0).Text, ListView1.Items(i).SubItems(1).Text)
                Next
    
                ReportViewer1.LocalReport.DataSources.Clear()
                ReportViewer1.LocalReport.DisplayName = "SampleReport"
                ReportViewer1.LocalReport.ReportPath = "SampleReport.rdlc"
                Dim rds As ReportDataSource = New ReportDataSource("DataSet1", dt)
                ReportViewer1.LocalReport.DataSources.Add(rds)
                Me.ReportViewer1.RefreshReport()
    
            Catch ex As Exception
                Dim errormsg As String = String.Empty
                errormsg = ex.ToString()
            End Try
        End Sub
    End Class


    • Edited by Tech Aspirant Tuesday, November 13, 2018 5:52 PM
    • Marked as answer by Amr_Aly Wednesday, November 14, 2018 6:54 AM
    Tuesday, November 13, 2018 5:52 PM

All replies

  • Hello,

    Every time Listview will have fixed no. of Rows in your case ? If yes then you can create parameters and use that ,

    If not then in that case I will suggest just create a dummy table in your database with no. of columns equal to no. of columns in listview.

    Then convert listview to datatable and then pass datatable content to rdlc report table like below

    Hope below code helps you

    CREATE TABLE [dbo].[DummyTable](
    	[ProductCode] [varchar](100) NULL,
    	[ProductName] [varchar](100) NULL
    ) ON [PRIMARY]
    Imports Microsoft.Reporting.WinForms
    
    Public Class Form17
    
        Private Sub Form17_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Me.ReportViewer1.RefreshReport()
            'Fill Listview
            Try
                Dim productcode As List(Of String) = {"P01", "P02", "P03", "P04", "P05"}.ToList()
                Dim productname As List(Of String) = {"PName1", "PName2", "PName3", "PName4", "PName5"}.ToList()
                Dim str(2) As String
                For i = 0 To 4
                    Dim itm As ListViewItem
                    str(0) = productcode(i)
                    str(1) = productname(i)
                    itm = New ListViewItem(str)
                    ListView1.Items.Insert(0, itm)
                Next
            Catch ex As Exception
                Dim errormsg As String = String.Empty
                errormsg = ex.ToString()
            End Try
        End Sub
        'generate report from Listview Items
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Try
                'Convert Listview to Datatable
                Dim dt As New DataTable
                dt.Columns.Add("ProductCode")
                dt.Columns.Add("ProductName")
                For i = 0 To ListView1.Items.Count - 1
                    dt.Rows.Add(ListView1.Items(i).SubItems(0).Text, ListView1.Items(i).SubItems(1).Text)
                Next
    
                ReportViewer1.LocalReport.DataSources.Clear()
                ReportViewer1.LocalReport.DisplayName = "SampleReport"
                ReportViewer1.LocalReport.ReportPath = "SampleReport.rdlc"
                Dim rds As ReportDataSource = New ReportDataSource("DataSet1", dt)
                ReportViewer1.LocalReport.DataSources.Add(rds)
                Me.ReportViewer1.RefreshReport()
    
            Catch ex As Exception
                Dim errormsg As String = String.Empty
                errormsg = ex.ToString()
            End Try
        End Sub
    End Class


    • Edited by Tech Aspirant Tuesday, November 13, 2018 5:52 PM
    • Marked as answer by Amr_Aly Wednesday, November 14, 2018 6:54 AM
    Tuesday, November 13, 2018 5:52 PM
  • Hello,

    Every time Listview will have fixed no. of Rows in your case ? If yes then you can create parameters and use that ,

    Thanks for your fast reply,

    but this code 

     For i = 0 To ListView1.Items.Count - 1
                If ListView1.Items.Count <> 0 Then
    
                    
                    With ListView1.Items.Item(i)
                        Dim productcode As New ReportParameter("ProductCodeParam", Val(.SubItems(1).Text))
                        Dim productparam As New ReportParameter("ProductParam", (.SubItems(2).Text))
                        Dim sprice As New ReportParameter("SellPriceParam", Val(.SubItems(5).Text))
                        Dim soldcartons As New ReportParameter("SoldCartonsParam", Val(.SubItems(6).Text))
                        Dim soldpackets As New ReportParameter("SoldPacketsParam", Val(.SubItems(7).Text))
                        Dim totalamount As New ReportParameter("TotalAmountParam", Val(.SubItems(8).Text))
                       
                        SearchForm15.ReportViewer1.LocalReport.SetParameters(New ReportParameter() {productcode})
                        SearchForm15.ReportViewer1.LocalReport.SetParameters(productparam)
                        SearchForm15.ReportViewer1.LocalReport.SetParameters(sprice)
                        SearchForm15.ReportViewer1.LocalReport.SetParameters(soldpackets)
                        SearchForm15.ReportViewer1.LocalReport.SetParameters(soldcartons)
                        SearchForm15.ReportViewer1.LocalReport.SetParameters(totalamount)
                    End With
                End If
            Next

    Is Already a parameter to my report viewer .... it gives me a repeated (one row) although i have 3 or 4 or five rows with 5 different products

    this my list view with 3 products

    The question here is how to loop with every row in the list view to get the report viewer equal to my list view .........

    Thanks ,

    • Edited by Amr_Aly Tuesday, November 13, 2018 7:12 PM
    Tuesday, November 13, 2018 7:02 PM
  • You want to use parameters ?
    Tuesday, November 13, 2018 7:07 PM
  • You want to use parameters ?
     Sorry I'm already using it but it failed to achieve the mission 

    Regards From Amr_Aly

    Tuesday, November 13, 2018 7:57 PM
  • Actually You want to display n number of rows but your code is replacing parameter value each time it initiates next loop. thats why you are seeing last value in parameters in report .rdlc

    I think in your case Table in rdlc should work as i have elaborated example above

     
    Tuesday, November 13, 2018 8:05 PM
  • Actually You want to display n number of rows but your code is replacing parameter value each time it initiates next loop. thats why you are seeing last value in parameters in report .rdlc

    I think in your case Table in rdlc should work as i have elaborated example above

     

    thanks , i am trying your code i think it is very near to solve this issue ...... but i have some problems with it 

    this is my code 

        Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
            Dim con As New OleDbConnection(cs)
            Dim da As New OleDbDataAdapter()
            Dim ds As New DataSet
            Dim cmd As New OleDbCommand("SELECT * FROM ListViewTable")
            cmd.CommandType = CommandType.Text
            cmd.Connection = con
            da.SelectCommand = cmd
            'ds.Clear()
            'da.Fill(ds, "DataSet1")
    
            'Convert Listview to Datatable
            Dim dt As New DataTable
            dt.Columns.Add("ProductCode")
            dt.Columns.Add("ProductName")
            dt.Columns.Add("Packets")
            dt.Columns.Add("Cartons")
            dt.Columns.Add("Price")
            dt.Columns.Add("TotalAmount")
    
            For i = 0 To ListView1.Items.Count - 1
                dt.Rows.Add(ListView1.Items(i).SubItems(1).Text,
                            ListView1.Items(i).SubItems(2).Text,
                            ListView1.Items(i).SubItems(5).Text,
                            ListView1.Items(i).SubItems(6).Text,
                            ListView1.Items(i).SubItems(7).Text,
                            ListView1.Items(i).SubItems(8).Text)
            Next
    
            'Dim rds As New ReportDataSource("DataSet1", ds.Tables(0))
    
            'SearchForm15.ReportViewer1.LocalReport.DataSources.Add(rds)
    
    
            SearchForm15.ReportViewer1.LocalReport.DataSources.Clear()
            SearchForm15.ReportViewer1.LocalReport.ReportPath = Path.Combine(Application.StartupPath, "E:\KMAStock\KMAStock\Report1.rdlc")
            Dim rds As ReportDataSource = New ReportDataSource("DataSet1", dt)
            SearchForm15.ReportViewer1.LocalReport.DataSources.Add(rds)
    
            Dim invoiceno As New ReportParameter("InvoiceNoParam", Me.txtInvoiceNo.Text)
            Dim solddate As New ReportParameter("SoldDateParam", Me.dtpTransactionDate.Text)
            Dim customername As New ReportParameter("CustomerNameParam", Me.cmbcustomername.Text)
    
    
            SearchForm15.ReportViewer1.LocalReport.SetParameters(invoiceno)
            SearchForm15.ReportViewer1.LocalReport.SetParameters(solddate)
            SearchForm15.ReportViewer1.LocalReport.SetParameters(customername)
    
    
    
            SearchForm15.TabControl1.SelectedTab = SearchForm15.TabPage4
            SearchForm15.ShowDialog()
            'SearchForm15.ReportViewer1.SaveReport()
            SearchForm15.ReportViewer1.Refresh()
            SearchForm15.ReportViewer1.RefreshReport()
            SearchForm15.ReportViewer1.LocalReport.Refresh()
    
        End Sub
    
        Sub lstprint()
            Try
                'Dim productcode As List(Of String) = {"P01", "P02", "P03", "P04", "P05"}.ToList()
                'Dim productname As List(Of String) = {"PName1", "PName2", "PName3", "PName4", "PName5"}.ToList()
                'Dim pakets As List(Of String) = {"Pac1", "Pac2", "Pac3", "Pac4", "Pac5"}.ToList()
                'Dim cartons As List(Of String) = {"C1", "C2", "C3", "C4", "C5"}.ToList()
                'Dim price As List(Of String) = {"Pr1", "Pr2", "Pr3", "Pr4", "Pr5"}.ToList()
                'Dim totalamount As List(Of String) = {"amt1", "amt2", "amt3", "amt4", "amt5"}.ToList()
                Dim productcode As List(Of String) = {"", "", "", "", ""}.ToList()
                Dim productname As List(Of String) = {"", "", "", "", ""}.ToList()
                Dim pakets As List(Of String) = {"", "", "", "", ""}.ToList()
                Dim cartons As List(Of String) = {"", "", "", "", ""}.ToList()
                Dim price As List(Of String) = {"", "", "", "", ""}.ToList()
                Dim totalamount As List(Of String) = {"", "", "", "", ""}.ToList()
                Dim str(6) As String
                For i = 1 To 6
                    Dim itm As ListViewItem
                    str(1) = productcode(i)
                    str(2) = productname(i)
                    str(3) = pakets(i)
                    str(4) = cartons(i)
                    str(5) = price(i)
                    str(6) = totalamount(i)
                    itm = New ListViewItem(str)
                    ListView1.Items.Insert(0, itm)
                Next
            Catch ex As Exception
                Dim errormsg As String = String.Empty
                errormsg = ex.ToString()
            End Try
        End Sub

    The error is 


    Regards From Amr_Aly

    Tuesday, November 13, 2018 9:06 PM
  • Many thanks Tech Aspirant

    finally you solved my issue ....... i removed Sub lstprint() from my code and everything become so nice

    no errors and list view items  passed to the report viewer smoothly

    Regards from Egypt sir  


    Regards From Amr_Aly

    Tuesday, November 13, 2018 9:44 PM
  • Hi,

    We appreciate that you shared your solution with us. Please mark the post that helped you as the answer.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 14, 2018 2:08 AM