how to refresh my datagridview after I add new data
Asked Answered
K

9

5

I'm having a lot of trouble finding ways to refresh my datagridview.. I've tried datagridview.refresh(), datagridview.Update()....but it doesn't work...

here's my code

Imports System.Data
Imports System.Data.OleDb
Imports System.Data.Odbc
Imports System.Data.DataTable
Public Class Form1

Dim provider As String
Dim dataFile As String
Dim connString As String
Dim addstring As String
Dim cnn As OleDbConnection = New OleDbConnection
Dim ds As DataSet = New DataSet
Dim da As OleDbDataAdapter
Dim tables As DataTableCollection = ds.Tables
Dim cmd As New OleDb.OleDbCommand
Dim dr As System.Data.OleDb.OleDbDataReader


Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    t_date.Text = Today
    provider = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="
    dataFile = "C:\Users\hp-2\Documents\Visual Studio 2012\Projects\Delta\Delta.mdb"

    connString = provider & dataFile
    cnn.ConnectionString = connString
    da = New OleDbDataAdapter("Select Customer_Name, Job, Amount from [Transaction] where Trans_date = Date()", cnn)
    da.Fill(ds, "Transaction")

    Dim view1 As New DataView(tables(0))
    Dim source1 As New BindingSource()
    source1.DataSource = view1
    showdata.DataSource = view1
    showdata.Refresh()
    cnn.Close()
End Sub

I've tried this one but it doesn't work too.

Private Sub showdat()
    If Not cnn.State = ConnectionState.Open Then
        cnn.Open()
    End If
    showdata.Refresh()
    cnn.Close()
End Sub

...

Private Sub btmclose_Click(sender As Object, e As EventArgs) Handles btmclose.Click
    Me.Close()
End Sub

Private Sub C_job_SelectedIndexChanged(sender As Object, e As EventArgs) Handles C_job.SelectedIndexChanged
    Dim selected As String = C_job.SelectedItem.ToString()
    If selected = "Internet" Then
        t_amount.Text = "20"
        php.Visible = True
    ElseIf selected = "Games" Then
        t_amount.Text = "10"
        php.Visible = True
    ElseIf selected = "Print (short)" Then
        t_amount.Text = "1"
        php.Visible = True
    ElseIf selected = "Print (long)" Then
        t_amount.Text = "2"
        php.Visible = True
    ElseIf t_amount.Text = "" Then
        php.Visible = False
    End If
End Sub

here is my ADD button... after i've click it...the data is successfully added but the datagridview doesn't refresh...

Private Sub btnadd_Click(sender As Object, e As EventArgs) Handles btnadd.Click

    provider = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
    dataFile = "C:\Users\hp-2\Documents\Visual Studio 2012\Projects\Delta\Delta.mdb"

    connString = provider & dataFile
    cnn.ConnectionString = connString
    cnn.Open()

    cmd.Connection = cnn

    cmd.CommandText = "insert into [Transaction] (Customer_Name, Job, Trans_date, Amount ) " & _
        " values ('" & C_name.Text & "','" & C_job.Text & "','" & t_date.Text & "','" & t_amount.Text & "')"
    cmd.ExecuteNonQuery()

    showdat()

    cnn.Close()
End Sub

End Class
Kazim answered 12/8, 2013 at 16:6 Comment(1)
are you even using a DataGrid? or are you trying to do this with a DataSet and a Table created from the DataSet rather than a DataGrid from a DataSet>?Disjunct
E
4

wish this will help Create Function

        private sub loaddata()
        datagridview.Datasource=nothing
        datagridview.refresh
        dim str as string = "select * from database"
      using cmd As New OleDb.OleDbCommand(str,cnn)
        using da As new OleDbDataAdapter(cmd)
          using newtable as new datatable
            da.fill (newtable)
            datagridview.datasource=newtable
          end using
       end using
     end using
end sub
Endeavor answered 28/3, 2017 at 9:2 Comment(0)
H
3

I think the problem is that you're adding a new entry to the database, but not the data structure that the datagrid represents. You're only querying the database for data in the load event, so if the database changes after that you're not going to know about it.

To solve the problem you need to either re-query the database after each insert, or add the item to tables(0) data structure in addition to the Access table after each insert.

Huberthuberto answered 12/8, 2013 at 16:19 Comment(3)
re-query? where in my ADD button code...so i need to copy the code inside my form_load to the Add button code so that it will update the datagrid?Kazim
Yup It actually work, but the problem is just double the display in the datagrid....example: there's a diplay of "1" in the gatagridview then when I add data "2" the display will be "1" under it is "2" then "1" again and "2"...Kazim
@hPys: Try just setting showdata.DataSource = tables(0). I think your view1 and source1 variables might be unnecessary.Huberthuberto
F
2

I found this code to work if you're trying to refresh a bound datagridview with updated data from a dataset. Obviously, this was after I sent the update to the database.

'clear out the datasource for the Grid view
Me.DataGridView1.DataSource = Nothing
'refill the table adapter from the dataset table 
Me.viewABCTableAdapter.Fill(Me.yourDataSet.viewABC)
'reset the datasource from the binding source
Me.DataGridView1.DataSource = Me.viewABCBindingSource
'should redraw with the new data
Me.DataGridView1.Refresh()
Faubert answered 31/5, 2015 at 20:11 Comment(0)
G
0

This reloads the datagridview:

Me.ABCListTableAdapter.Fill(Me.ABCLISTDATASET.ABCList)

Hope this helps

Gyatt answered 31/10, 2013 at 20:54 Comment(0)
S
0

You can use a binding source to bind to with your datagridview. Set your class or list of data. Set a bindingsource.datasource equal to that. Set the datasource of your datagridview to your bindingsource.

Seedling answered 13/3, 2014 at 18:26 Comment(0)
B
0

If you using formview or something similar you can databind the gridview on the iteminserted event of the formview too. Like below

protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e)
    {
        GridView1.DataBind();
    }

You can do this on the data source iteminserted too.

Beforetime answered 27/2, 2015 at 17:2 Comment(0)
J
0

reload the form

Form1_Load(sender, e)
Janejanean answered 17/1, 2017 at 14:4 Comment(0)
W
0

In the code of the button that saves the changes to the database eg the update button, add the following lines of code:

MyDataGridView.DataSource = MyTableBindingSource

MyDataGridView.Update()

MyDataGridView.RefreshEdit()
Wist answered 7/1, 2020 at 13:0 Comment(0)
J
-2
this.tablenameTableAdapter.Fill(this.databasenameDataSet.tablename)
Jaramillo answered 5/4, 2016 at 11:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.