I have small issue with my datagridview and crystal report

I am using this code for display my crystal report but my problem is I can display only one row from data grid view

But I want to print all rows randomly selected from datagridview in crystal report

Private Function MontoringInfo () As IMS
        Try
            Dim objConn As SqlConnection = New SqlConnection(strConn)
            objConn.Open()
            Dim objComm As SqlCommand = New SqlCommand()
            For Each row As DataGridViewRow In dataGridView1.Rows
                For i As Integer = 0 To dataGridView1.Rows.Count - 124
                    objComm.CommandText = "SELECT * FROM table1 where Upazila = '" & dataGridView1.SelectedRows(i).Cells(2).Value.ToString() & "'"
                Next
            Next
            objComm.Connection = objConn
            objComm.CommandType = CommandType.Text
            Dim da As SqlDataAdapter = New SqlDataAdapter(objComm)
            Dim ds As IMS = New IMS()
            da.Fill(ds, " table1 ")
            Return ds
        Catch ex As SqlException
            MsgBox(ex.Message, MsgBoxStyle.OkOnly, "Error")
            Return Nothing
        End Try
    End Function
    Private Sub btnprint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnrr.Click
        Dim cry As ReportClass = New Data_main
        cry.SetDataSource(MontoringInfo ())
        Dim rc As New Reportviewer(cry)
        rc.ShowDialog()

Here Is My Process Of Application

I have clicked on load button and it loads all data from database to datagridview

After that when i click on particular row its display only one row in crystal report but i want to display all current rows which was in datagriwview

Here What's Happen

First I have Clicked ON Row in datagridview

It shows in crystal report one row but i want to print all rows current rows

Please Help what should i do print all current rows thank you

Recommended Answers

All 3 Replies

Wrong cods to construct SQL Statement.

For Each row As DataGridViewRow In dataGridView1.Rows
For i As Integer = 0 To dataGridView1.Rows.Count - 124
objComm.CommandText = "SELECT * FROM table1 where Upazila = '" & dataGridView1.SelectedRows(i).Cells(2).Value.ToString() & "'"
Next
Next
Do not understand, why did you use those nested loops.
After completing it always gives you a single row.

commented: yes you are right ,it gives me only single row,but how can i solved this problem,I want to randomly select some rows from gridview and display it to crystal report.plse. help if u can. +0

yes you are right ,it gives me only single row,but how can i solved this problem,I want to randomly select some rows from gridview and display it to crystal report.plse. help if u can.

Ha! downvoted.

Ok, you posted

First I have Clicked ON Row in datagridview

It shows in crystal report one row but i want to print all rows current rows

What do you mean by all rows current rows?

And from your second post

I want to randomly select some rows from gridview and display it to crystal report

What do you mean by randomly select some rows?

You can randomly select multiple rows in a DataGridView control by using Ctrl key and left mousebutton, if DataGridView's multiSelect property is already set to True.

But you say "First I have Clicked ON Row in datagridview".
It is not possible. When you clicked on a row multisecection is discarded and it represents only a single row, on which you clicked.

From my opinion you can do it by the following steps.
1) Create a seperate table with which fields you want to display.
2) Transfer data from table1 to the new table or save data of selected cells of Datagridview to the new table.
3) Pass this new table to show the result.

All procedures will be called from a Button Click event or anywhwre not from any events of DataGridView.

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.