Hi,

I'm looking to find out how to force a refresh on a datagrid in a windows app. On a mainForm the user clicks a button to add a new record. This opens a new window. When the user has entered the data they click save and close which should commit the data and close the new window.

I also need to refresh the original datagrid so the new information is present. Ive tried invalidate(), and also endEdit() with refresh() on the datagrid but nothing seems to be working.

Can anyone shed any light?

Recommended Answers

All 5 Replies

Call the fill method of the bound data table, something that looks like this
TableAdapter.Fill(Dataset.Table)

Checking if a certain form was closed should be easy.

thank you for aswering
with this i get "Object reference not set to an instance of an object."

i corrected it but it will not show the new data, i still have to stop and rerun the application to see the new rows.

If you used a DataAdapter DataSet and bound your datagrid with a BindingSource all you would have to do is call bindingSource1.ResetBindings(False) you would get the functionality you're looking for. Here is some sample code to point you in that direction.

Imports System.Data.OleDb

Public Class Form1


    Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\yourAccess.mdb")
    Dim bs As New BindingSource
    Dim ds As New DataSet
    Dim da As New OleDbDataAdapter()

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim sql As String = "Select * From tbl_Inventory Order By Item_Name ASC;"

        Dim cmd As New OleDbCommand()
        With cmd
            .CommandType = CommandType.Text
            .CommandText = sql
            .Connection = conn
        End With

        With da
            .SelectCommand = cmd
            .Fill(ds, "tbl_Inventory")
        End With
        Dim cmdBuilder As New OleDbCommandBuilder(da)

        bs.DataSource = ds.Tables(0)

        dgv1.DataSource = bs

        dgv1.Columns(0).ReadOnly = True
        dgv1.Columns(0).HeaderText = "ID"
        dgv1.Columns(1).ReadOnly = True
        dgv1.Columns(1).HeaderText = "ITEM NAME"
        dgv1.Columns(2).Visible = False
        dgv1.Columns(3).Visible = False
        dgv1.Columns(4).Visible = False
        dgv1.Columns(5).Visible = False
        dgv1.Columns(6).Visible = False
        dgv1.Columns(7).HeaderText = "ACTIVE"

    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        Try
            bs.ReSetBindings(False) 'Updates the DataGridView
            da.Update(ds, "tbl_Inventory") 'Updates the Access DataTable

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

End Class

Just refresh your dataset / datatable and the set the datasource. That will automatically refresh you

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.