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

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.

Related

Import Excel file to Datagridview Vb.net

Is there a way to display the excel file directly to the Datagridview without the combobox or choosing the sheets to display? Since my excel file contains only 1 sheet.
Dim tables As DataTableCollection
Private Sub btnBrowse_Click(sender As Object, e As EventArgs) Handles btnBrowse.Click
Using ofd As OpenFileDialog = New OpenFileDialog() With {.Filter = "Excel 97-2003 Workbook|*.xls|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
Dim dt As DataTable = tables(cboSheet.SelectedItem.ToString())
DataGridView1.DataSource = dt
End Sub

Pass DataTable to ReportViewer

I am trying to pass a datatable to a reportviewer which I fill by code, is there a way to do that? I tried this but nothing happened:
Dim bs As BindingSource
bs = New BindingSource()
bs.DataSource = DataTablefillbycode
Dim rs As ReportDataSource
rs = New ReportDataSource()
rs.Name = "Tabletest"
rs.Value = bs
form2.ReportViewer1.RefreshReport()
form2.ReportViewer1.Reset()
form2.ReportViewer1.LocalReport.ReportEmbeddedResource = "Test.Report1.rdlc"
form2.ReportViewer1.LocalReport.DataSources.Clear()
form2.ReportViewer1.LocalReport.DataSources.Add(rs)
form2.ReportViewer1.RefreshReport()
form2.ShowDialog()
PS : The GridView works fine with the table "Tablefillbycode"
Follow these steps to be able to pass data table to your report:
I suppose you created a Report1.rdlc in root of your project Test, so the name of its embedded resource would be Test.Report1.rdlc. Also I suppose the name of DataSet in your Report1 is DataSet1.
Put a report viewer on your Form2 and set its Dock property to Fill and set its Modifier property to Public.
In Form1 I suppose you have a DataGridView1 that you want to fill it in the Form_Load and you will use the same query that you used for creating report.
In Form1 I suppose you have a Button1 that you want to show Form2 when you click on Button1 and you want pass the data of DataGridView1 to it.
Don't forget to Imports Microsoft.Reporting.WinForms in Form1
Code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim cn = "data source=(localdb)\v11.0;initial catalog=TestDB;integrated security=True;"
Dim cmd = "SELECT Id,Name FROM Category"
Dim adapter = New SqlDataAdapter(cmd, cn)
Dim table = New DataTable()
adapter.Fill(table)
Me.DataGridView1.DataSource = table
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim form2 = New Form2()
Dim rds= New ReportDataSource("DataSet1", Me.DataGridView1.DataSource)
form2.ReportViewer1.LocalReport.DataSources.Clear()
form2.ReportViewer1.LocalReport.DataSources.Add(rds)
form2.ReportViewer1.LocalReport.ReportEmbeddedResource = "Test.Report1.rdlc"
form2.ShowDialog()
End Sub
Screenshot:

Crystal Report Data source from Datagridview datasource

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

How to save data within the Application?

I'm creating a basic To-do program. I want to save the list of tasks the user creates. Now I've tried with saving them as text files but I don't want it that way. I want it so that I can save the tasks the user creates within the program rather than external text files and then retrieve those saved files and display them in a text file.
Essentially I need a way to save data without needing to rely on databases.
A good example is GeeTeeDee. It seems to be saving its files and data etc. in the program within rather than external text file.(I'm assuming this because I can't seem find them. I could be wrong)
Update
I was doing a bit of searching can came across this: Click here!!!
But the problem is that I'm confused as to how this works. Is someone able to clear things for me? It would be GREATLY appreciated as it's exactly what I'm looking for.
The "code project" example saves the data at an external file with extension [*.brd] .
You can Use XmlSerializer to save and load your data from external xml file with extension xml,brd or anything else.
Try the code below, add into a form1 three buttons (Button1,Button2,Button3) and a DataGridView1, paste the code and Run.
press button "add data dynamically" or/and add,edit,delete row directlly from DataGridView1.
press Save data.
close and run programm
press Load data.
Imports System.Xml.Serialization
Imports System.IO
Class Form1
Dim ds As DataSet
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Button1.Text = "Load Data"
Button2.Text = "add data dynamically"
Button3.Text = "Save Data"
'Create Dataset
ds = CreateDataset()
'Set DataGridView1
DataGridView1.DataSource = ds.Tables("Person")
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
LoadFromXMLfile("c:\temp\persons.xml")
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
AddDataToDataSetDynamically(ds)
End Sub
Private Sub Button3_Click_1(sender As System.Object, e As System.EventArgs) Handles Button3.Click
SaveToXMLFile("c:\temp\persons.xml", ds)
End Sub
Private Function CreateDataset() As DataSet
Dim dataset1 As New DataSet("Persons")
Dim table1 As New DataTable("Person")
table1.Columns.Add("Id")
table1.Columns.Add("FName")
table1.Columns.Add("Age")
'...
dataset1.Tables.Add(table1)
Return dataset1
End Function
Private Sub AddDataToDataSetDynamically(d As DataSet)
d.Tables("Person").Rows.Add(1, "Andrew", "46")
d.Tables("Person").Rows.Add(2, "Nicky", "43")
d.Tables("Person").Rows.Add(3, "Helen", "15")
End Sub
Private Sub SaveToXMLFile(filename As String, d As DataSet)
Dim ser As XmlSerializer = New XmlSerializer(GetType(DataSet))
Dim writer As TextWriter = New StreamWriter(filename)
ser.Serialize(writer, d)
writer.Close()
End Sub
Private Sub LoadFromXMLfile(filename As String)
If System.IO.File.Exists(filename) Then
Dim xmlSerializer As XmlSerializer = New XmlSerializer(ds.GetType)
Dim readStream As FileStream = New FileStream(filename, FileMode.Open)
ds = CType(xmlSerializer.Deserialize(readStream), DataSet)
readStream.Close()
DataGridView1.DataSource = ds.Tables("Person")
Else
MsgBox("file not found! add data and press save button first.", MsgBoxStyle.Exclamation, "")
End If
End Sub
End Class
add that code to form1 and get the data to a textbox (add button4,textbox1)
Private Function PrintRows(dataSet As DataSet) As String
Dim s As String = ""
Dim thisTable As DataTable
For Each thisTable In dataSet.Tables
Dim row As DataRow
For Each row In thisTable.Rows
Dim column As DataColumn
For Each column In thisTable.Columns
s &= row(column) & " "
Next column
s &= vbCrLf
Next row
Next thisTable
Return s
End Function
Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
TextBox1.Text = PrintRows(ds)
End Sub

Real Time Updating winforms visual studio 2010 .net

I am having some problems getting the data to reload after its updated to the database i am loading my gridview from form load BindGrid() event I have included my code here.
My Delcarations are as follows I have tried everything here but cant force it to refresh the grid
Dim dbContext As New R3Delivery
Dim threeContext As New skechersDeliveryEntities1
Dim bs As New BindingSource
Private Sub frmConfirmDeliverys_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
BindGrid()
dgDeliverys.Columns(0).ReadOnly = True
dgDeliverys.Columns(1).ReadOnly = True
dgDeliverys.Columns(2).ReadOnly = True
End Sub
Public Sub BindGrid()
Dim cfglocation As Int16
cfglocation = cfb.StoreLocation
bs.DataSource = (From u In threeContext.R3Delivery Where u.isprocessed = True AndAlso u.location = 1
Select u)
bs.ResetBindings(True)
dgDeliverys.DataSource = bs
End Sub
My save button is as follows
Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
threeContext.SaveChanges()
BindGrid()
End Sub
I thought I should show my declaration of my form as well the above code is in my edit form the below is my calling from
Dim frmConfirmDeliverys As New frmConfirmDeliverys
frmConfirmDeliverys.ShowInTaskbar = False
frmConfirmDeliverys.ShowDialog()
have you tried doing this?
dgDeliverys.Datasource=Nothing
dgDeliverys.DataSource = bs
or
dgDeliverys.Refresh
Also like jmcilhinney said, check if the data you are expecting is returned by the query.