Crystal Report Data source from Datagridview datasource - vb.net

Can anyone help me? I have a project in VB.NET and trying to show to my "CrystalReportViewer1" then I set datasource from this datagridview "MenuTambah.DGVTambah.DataSource".
I create "CrystalReport1.rpt" in project (project > add new item> crystal report and named it "CrystalReport1.rpt")
this is the code when my form load
Private Sub LaporanViewer_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim crReportDocument As New CrystalReport1
crReportDocument.SetDataSource(MenuTambah.DGVTambah.DataSource)
CrystalReportViewer1.RefreshReport()
'View the report
CrystalReportViewer1.ReportSource = crReportDocument
End Sub
I have successfully loaded my database table in that Datagridview in other form called "MenuTambah.DGVTambah" then I want to set my crystal document datasource based on my datagridview with code above. When run and when "MenuTambah" load, there is no exception error or something, just exit, any idea?

Try this
Click on: Project > Your Project Propertise > Settings
Public Sub ShowReport(ByVal MyReport As CrystalDecisions.CrystalReports.Engine.ReportDocument, ByVal filterstring As String, ByVal CrystalReportViewer As CrystalDecisions.Windows.Forms.CrystalReportViewer)
Dim myLogonInfo As New CrystalDecisions.Shared.TableLogOnInfo
Dim myTable As Table
For Each myTable In MyReport.Database.Tables
myLogonInfo = myTable.LogOnInfo
myLogonInfo.ConnectionInfo.ServerName = My.Settings.RptserverPath.ToString
myLogonInfo.ConnectionInfo.DatabaseName = My.Settings.Database.ToString
myLogonInfo.ConnectionInfo.UserID = My.Settings.DBUser.ToString
myLogonInfo.ConnectionInfo.Password = My.Settings.DBPass.ToString
myTable.ApplyLogOnInfo(myLogonInfo)
Next myTable
CrystalReportViewer.ReportSource = MyReport
CrystalReportViewer.SelectionFormula = filterstring
CrystalReportViewer.Refresh()
End Sub
Private Sub SimpleButton6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton6.Click
Dim MyReport As CrystalDecisions.CrystalReports.Engine.ReportDocument = New 'Your Report Name'
ShowReport(MyReport, filterstring, CrystalReportViewer1)
End Sub

Related

DataTable to Crystal Report

I'm trying to print the conents of DataGrid into CrystalReport by passing data from DGV to Datatable then to Crystal Report, it works but my problem is it only displays the first row in the crystal report... I need all the rows to be printed.. here's the code below:
'
Dim dt_RadLogBook As New DataTable("dt_RadLogBook")
With dt_RadLogBook
.Columns.Add("CaseNumber")
.Columns.Add("Name")
.Columns.Add("BirthDate")
.Columns.Add("PhilHealth Membership")
.Columns.Add("Ward Name")
.Columns.Add("Address")
.Columns.Add("LMP")
End With
For Each dgr As DataGridViewRow In frmRadLogBook.dgvLogBook.Rows
dt_RadLogBook.Rows.Add(dgr.Cells(0).Value, dgr.Cells(1).Value, dgr.Cells(2).Value, dgr.Cells(3).Value, dgr.Cells(4).Value, dgr.Cells(5).Value, dgr.Cells(6).Value)
MsgBox(dgr.Cells(0).ToString)
Next
Dim rptDocument As CrystalDecisions.CrystalReports.Engine.ReportDocument
rptDocument = New rptRadLogBook1
rptDocument.SetDataSource(dt_RadLogBook)
frmReportForm.CRV1.ReportSource = rptDocument
frmReportForm.ShowDialog()
'
Instead of re-inventing the wheel, you can extract the DataSource from the DataGridView.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click
Dim dt = DirectCast(frmRadLogBook.dgvLogBook.DataSource, DataTable)
Dim rptDocument As CrystalDecisions.CrystalReports.Engine.ReportDocument
rptDocument = New rptRadLogBook1
rptDocument.SetDataSource(dt)
frmReportForm.CRV1.ReportSource = rptDocument
frmReportForm.ShowDialog()
End Sub
I don't have Crystal Reports so I can not test the rest of the code.

VB.NET How to Update DataSource from DataGridView (Binding to Dataset.DataTable)

Halo All..
I'm working with Visual Studio 2019 to create WinFormApp
What i'm working with as follow:
Adding New DataSource from wizard which connect to Acces Database (store locally within app), Acces Database name is Supplier with only Headers (9 Headers) and no data.
Adding DataGridView to Form and bind it to SupplierDataSet.SupplierDataTable
Adding OpenFileDialog where users can browse Excel File then select Sheet Name and view the data in DataGridView.
Adding Import Button to save/update SupplierDataSet.SupplierDataTable
Here is my code:
Imports System.IO
Imports ExcelDataReader
Public Class Form2
Dim tables As DataTableCollection
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub BtnBrowse_Click(sender As Object, e As EventArgs) Handles BtnBrowse.Click
'read excel file
Using ofd As OpenFileDialog = New OpenFileDialog() With
{.Filter = "Excel Workbook|*.xlsx"}
If ofd.ShowDialog() = DialogResult.OK Then
TxtFileName.Text = ofd.FileName
Using stream = File.Open(ofd.FileName, FileMode.Open, FileAccess.Read)
Using reader As IExcelDataReader = ExcelReaderFactory.CreateReader(stream)
Dim result As DataSet = reader.AsDataSet(New ExcelDataSetConfiguration() With {
.ConfigureDataTable = Function(__) New ExcelDataTableConfiguration() With {
.UseHeaderRow = True}})
tables = result.Tables
CboSheet.Items.Clear()
For Each table As DataTable In tables
CboSheet.Items.Add(table.TableName)
Next
End Using
End Using
End If
End Using
End Sub
Private Sub CboSheet_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CboSheet.SelectedIndexChanged
'Pupulate excel file data to DataGridView
Dim dt As DataTable = tables(CboSheet.SelectedItem.ToString())
DataGridView1.DataSource = dt
End Sub
Private Sub BtnImport_Click(sender As Object, e As EventArgs) Handles BtnImport.Click
'Here the code which I want to save/update SupplierDataSet.SupplierDataTable.
End Sub
End Class
Since i'm new for this (approx 2 weeks). I've no clue to how to update the DataTable
Please Help Me. Any help will be appreciated.
And So Many Thanks Before.

Filtering Datagridview From Datagridview Not From Database

i want to filter datagridview on my form..
on the form :
1 datagridview
1 label
1 timer
i have loaded database into datagridview (all data to datagridview)
on my datagridview i have 7 column the last column is date with format dd/MM/yyyy, and now how to filtering datagridview with label, i set this label to date like this
Private Sub TimerDate_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerDate.Tick
Dim FDate As String = Format(Today, "dd/MM/yyyy")
LblDate.Text = FDate
End Sub
and i want to eliminate the other data.. sooo in the end in my datagridview i have data with the last column same as LblDate.text
i dont want to filter datagridview from database.
can someone help me..? thanks.
sorry for my bad english.
this is how i populating data to datagrid
Public Class FrmJadwalSidang
Dim ConnString As String = ("Dsn=SqlConn;Server=192.168.100.1;uid=XXX;pwd=XXX;database=DBXXX;port=3306")
Public Function FillData(ByVal Sqlstring As String)
Dim OdbcConn As OdbcConnection = New OdbcConnection(ConnString)
OdbcConn.Open()
Dim MyDataSet As DataSet = New DataSet()
Dim MyOdbcdAdapter As OdbcDataAdapter = New OdbcDataAdapter()
MyOdbcdAdapter.SelectCommand = New OdbcCommand(Sqlstring, OdbcConn)
MyOdbcdAdapter.Fill(MyDataSet)
Me.DATAGRIDVIEW.DataSource = MyDataSet.Tables(0)
MyOdbcdAdapter.Dispose()
MyDataSet.Dispose()
OdbcConn.Close()
OdbcConn.Dispose()
End Function
Private Sub FrmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
FillData("Select nomor_perkara, jam_sidang, para_pihak, majelis_hakim_text, panitera_pengganti_text, agenda, tanggal_sidang from v_jadwal_sidang")
End Sub
End Class
SOLVED
Private Sub FrmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
FillData("Select nomor_perkara, jam_sidang, para_pihak, majelis_hakim_text, panitera_pengganti_text, agenda, tanggal_sidang from v_jadwal_sidang WHERE jadwal_sidang='" & LblDate.text.tostring & "'")
end sub
it work. in the end i have to filtering by the sql query..
thanks to someone who gives me the answer.
if you are using a SqlDataSource, set the FilterExpression of the control.
See this: How to: Enable Filtering for the SqlDataSource Control
And this: How to: Connect to an ODBC Database Using the SqlDataSource Control
EDIT : I provided the information for a web application not a winform application. Please read this for info on how to set up filtering for a BindingSource: BindingSource.Filter Property

Can not Search in and Edit DB in the same times (and same Windows Form) by using DataGridView

I'm have DataGridView in a Windows Form with some data in it and I have button for edit, I want to search for row by using TextBox and button and when I find the wanted row I will change it and click edit button,when I edit any row (without using search button) and press edit the DB is Updated , my problem is that: when I search for specific row and find it then edit the data and press edit button the data in DB don't updated, please help, I'm use this code:
Imports System.Data.SqlClient
Public Class Form9
Private sqlConn As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=E:\Clinic System\Clinic System\ClinicDB.mdf;Integrated Security=True;User Instance=True")
Private cmdSelect, cmdDelete As String
Private daEmployees As New SqlDataAdapter("Select * From History", sqlConn)
Private sqlCmndBuilder As New SqlCommandBuilder(daEmployees)
Private myDS As New DataSet
Private Sub HistoryBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.Validate()
Me.HistoryBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.ClinicDBDataSet3)
End Sub
Private Sub Form9_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
daEmployees.Fill(myDS, "History")
HistoryDataGridView.DataSource = myDS.Tables(0)
End Sub
Private Sub ButtonSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSearch.Click
Try
Dim con As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=E:\Clinic System\Clinic System\ClinicDB.mdf;Integrated Security=True;User Instance=True")
Dim d1 As New SqlDataAdapter("Select * from History Where Name like '%" & TextBox1.Text & "%'", con)
Dim d2 As New DataTable
d1.Fill(d2)
HistoryDataGridView.DataSource = d2
Catch ex As Exception
MessageBox.Show("Err.Discription")
End Try
End Sub
Private Sub ButtonEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonEdit.Click
daEmployees.Update(myDS.Tables(0))
MsgBox("Patient Details Updated!")
End Sub
Your issue is rather simple - you trying to update wrong table
daEmployees.Update(myDS.Tables(0))
While you need to update d2
In form scope
Dim _isSearch as boolean
On Form9_Load and when you reload as well
_isSearch = false
Here what you can do - In ButtonSearch_Click
_isSearch = true
If myDS.Tables.Contains("SEARCH_TBL") Then
myDS.Tables.Remove("SEARCH_TBL")
End if
Dim d2 As DataTable = myDS.Tables.Add("SEARCH_TBL")
d1.Fill(d2)
In ButtonEdit_Click
if _isSearch then
daEmployees.Update(myDS.Tables("SEARCH_TBL"))
else
daEmployees.Update(myDS.Tables(0))
End if
I think, daEmployees should update "SEARCH_TBL" because result set identical to first table. If not, just take your other adapter to a form scope.
But really, you can reuse the grid and update button, but you need to create logic tracking which table is currently loaded.

Public variable used for form opening not feeding through to from

Having some issues getting a form to populate based on a variable determined in current form.
I have a search result form that has a datagrid with all results, with an open form button for each row. When the user clicks this, the rowindex is used to pull out the ID of that record, which then feeds to the newly opened form and populates based on a SQL stored procedure run using the ID as a paramter.
However, at the moment the variable is not feeding through to the form, and am lost as to why that is. Stored procedure runs fine if i set the id within the code. Here is my form open code, with sci
Public Class SearchForm
Dim Open As New FormOpen
Dim data As New SQLConn
Public scid As Integer
Private Sub Search_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim sql As New SQLConn
Call sql.SearchData()
dgvSearch.DataSource = sql.dt.Tables(0)
End Sub
Private Sub dgvSearch_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvSearch.CellContentClick
Dim rowindex As Integer
Dim oform As New SprinklerCardOpen
rowindex = e.RowIndex.ToString
scid = dgvSearch.Rows(rowindex).Cells(1).Value
TextBox1.Text = scid
If e.ColumnIndex = 0 Then
oform.Show()
End If
End Sub
End Class
The form opening then has the follwing:
Private Sub SprinklerCard_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Populate fields from SQL
Try
Call Populate.SprinklerCardPopulate(ID)
cboInsured.Text = Populate.dt.Tables(0).Rows(0).Item(1)
txtAddress.Text = Populate.dt.Tables(0).Rows(0).Item(2)
txtContactName.Text = Populate.dt.Tables(0).Rows(0).Item(3)
txtContactPhone.Text = Populate.dt.Tables(0).Rows(0).Item(4)
txtContactEmail.Text = Populate.dt.Tables(0).Rows(0).Item(5)
numPumps.Value = Populate.dt.Tables(0).Rows(0).Item(6)
numValves.Value = Populate.dt.Tables(0).Rows(0).Item(7)
cboLeadFollow.Text = Populate.dt.Tables(0).Rows(0).Item(8)
cboImpairment.Text = Populate.dt.Tables(0).Rows(0).Item(9)
txtComments.Text = Populate.dt.Tables(0).Rows(0).Item(10)
Catch ex As Exception
MsgBox(ex.ToString & "SCID = " & ID)
End Try
End Sub
Set the ID variable in the form before you open it.
If e.ColumnIndex = 0 Then
oform.ID = scid
oform.Show()
End If