How to get current Selected Row in dataGridview and print using crystal report?
Print Only selected current row in data gridview

Recommended Answers

All 12 Replies

How to get current Selected Row in dataGridview

try this :

Dim i As Integer = CInt(ListView1.SelectedItems(0).Index)

TextBox1.Text = ListView1.Items(i).SubItems(0).Text
TextBox2.Text = ListView1.Items(i).SubItems(1).Text

@Jx Man he said datagridview not listview bro

i have no idea with CR, but i can do that with ReportViewer from .NET itself, if you want it i will show you how to do that

@Jx Man he said datagridview not listview bro

My bad.. Here's for the datagridview :

Dim i As Integer = DataGridView1.CurrentRow.Index
 With DataGridView1
     TextBox1.Text = DataGridView1.Item(0, i).Value
     TextBox2.Text = DataGridView1.Item(1, i).Value
 With

Hi @artemix22 can you do that?
using reportviewer

you only need to pass it to datatable and dataset, i think it can be done with CR too (with same technique) but you'll need to change a few line of code because i never use CR, here is what i do with ReportViewer :

  1. i add a DataGridView1 (i add two column for example) to Form1 and a Button too (Print Preview Button).
  2. i add another Form (Form2) to hold ReportViewer.
  3. still on Form2, i add a ReportViewer (set it to dock in parent container with clicking little arrow on right top corner of ReportViewer).
  4. i add a DataSet (click Project>Add New Item.. on Data template choose DataSet).
  5. i add a Report.rdlc (click Project>Add New Item.. on Reporting template choose Report or Report Wizard, i use Report).
  6. go to your DataSet1, right click than choose Add>Data Table.
  7. right click on DataTable1, and choose Add>Column or you can use ctrl+L, if you have two column on your DataGridView in Form1 than you have to add two column too on DataTable1 (amount of column on DataTable1 = amount of column on DataGridview1), name it what ever you want, default is DataColumn1, DataColumn2, and so on..).
  8. go to your Form1, on Print Preview Button add this code :

    Try
    Dim dtReport As New DataTable
    With dtReport
    .Columns.Add("DataColumn1")
    .Columns.Add("DataColumn2")
    End With
    dtReport.Rows.Add(DataGridView1.CurrentRow.Cells(0).Value, DataGridView1.CurrentRow.Cells(1).Value)
    Form2.ReportViewer1.LocalReport.DataSources.Item(0).Value = dtReport
    Form2.ShowDialog()
    Form2.Dispose()
    Catch ex As Exception
    MessageBox.Show(ex.Message)
    End Try

  9. go to your Report.rdlc, on top left corner click "New" then choose "Dataset..", on DataSource choose DataSet1 that you add before, click ok.

  10. still on your Report.rdlc, add a Table from ToolBox, you can delete the header, left click on first cell, you will see small blue icon, click it and choose DataColumn1, DataColumn2 for second cell.
  11. save all, then run it, it will look like this :

Example1
Example2
Example3

i think you can do same way for CR, but you'll need to change this part :

Form2.ReportViewer1.LocalReport.DataSources.Item(0).Value = dtReport

unfortunately it seems not support for multiple select on datagridview.

@artemix22 tnx tnx :)

sir @artemix22 can you also know the code for display all the datas in gridview???

it mean you have to read all data on your datagridview, you can do that with for looping, on example above, change this line :

dtReport.Rows.Add(DataGridView1.CurrentRow.Cells(0).Value, DataGridView1.CurrentRow.Cells(1).Value)

with this one :

 For Each row As DataGridViewRow In DataGridView1.Rows
      dtReport.Rows.Add(row.Cells(0).Value, row.Cells(1).Value)
 Next

not tested yet but i think it should work

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.