Reputation: 41
I have DataGridView which doesn't refresh. I was hoping to be able to refresh it by clicking on the 'Refresh' button.
My database is called 'ProjectDatabase' and the table I want to use is called 'SWOT'.
I'v tried a few ways but none have been working, and if it does work then I get new issues like every time I click on refresh it duplicates the data. Im using a MS Acess Database.
I tried the below coding, it refreshes, but duplicates the data x2 every time I click Refresh:
Private Sub btnRefresh_Click(sender As Object, e As EventArgs) Handles btnRefresh.Click
Refreshdata()
End Sub
Dim myConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\ahmed\OneDrive\Desktop\ProjectDatabase2003.mdb")
Dim DS As DataSet = New DataSet
Dim DA As OleDbDataAdapter
Dim tables As DataTableCollection = DS.Tables
Dim source1 As New BindingSource()
Private Sub Refreshdata()
DA = New OleDbDataAdapter("Select * from SWOT", myConnection)
DA.Fill(DS, "SWOT_Report")
Dim view1 As New DataView(tables(0))
source1.DataSource = view1
DataGridView1.DataSource = view1
DataGridView1.Refresh()
End Sub
Upvotes: 1
Views: 1362
Reputation: 6969
What you need to do is clear your DataTable
before filling it again. .NET doesn't do that automatically.
Private Sub Refreshdata()
'-- clear table before filling
If DS.Tables("SWOT_Report") IsNot Nothing Then DS.Tables("SWOT_Report").Rows.Clear
DA = New OleDbDataAdapter("Select * from SWOT", myConnection)
DA.Fill(DS, "SWOT_Report")
Dim view1 As New DataView(tables(0))
source1.DataSource = view1
DataGridView1.DataSource = view1
DataGridView1.Refresh()
End Sub
Upvotes: 1
Reputation: 3271
I think that you are overcomplicating your code with the use of the BindingSource
, DataSet
, DataAdapter
, and DataTable
member (class scope) variables.
For the member variables, strip them down to these two:
Dim ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\ahmed\OneDrive\Desktop\ProjectDatabase2003.mdb"
Dim swotBindingSource As BindingSource
Please note, the myConnection
object has been removed and replaced with a Connection String variable instead
In the Forms New()
method add:
swotBindingSource = New BindingSource()
DataGridView1.DataSource = swotBindingSource
This will setup your DataGridView
to have the swotBindingSource
as its DataSource. The BindingSource can be of use later on when it comes to filtering data.
And, finally change the RefreshData
sub to be:
Private Sub Refreshdata()
Dim swotDataTable As New DataTable
Using conn As New OleDbConnection(ConnectionString)
Using DA As New OleDbDataAdapter("SELECT * FROM SWOT", conn)
Dim ds As New DataSet
DA.Fill(ds, "SWOT_Report")
swotBindingSource.DataSource = ds.Tables(0)
End Using
End Using
End Sub
When the swotBindingSource
gets updated, you should automatically see the updates in the DataGridView.
It is likely you will also have to make other changes to the rest of your code due to the lack of myConnection
object, you will be better off creating a new Connection object (similar to how it is done in the RefreshData
sub) each time you need to use the database rather than creating one when the application starts and re-using the same connection.
Upvotes: 2