Hi guys,

I need help in refreshing the data in datagrid view, I’ve tried datagrid.refresh but nothing happens, I have to close the form and open it again to view the data. I need the data that I add to automatically show on the datagrid without having to close the form and opening it again.

Thanks.

Recommended Answers

All 6 Replies

Please post some code and some more information. Are you adding the data using datagridview? Or on another form?

Im using datagridview to display data. Below is my code:

Dim conn As OleDb.OleDbConnection
Dim cmd As OleDb.OleDbCommand
Dim insertQuery As String
Dim icount As Integer
Dim dr As OleDb.OleDbDataReader
Dim selectQuery As String

    If txtUserID.Text <> "" Then
        If txtPassword.Text <> "" Then
            If txtPassword.Text.Length < 8 Then
                MessageBox.Show("Please Enter a minimum of 8 characters for password", "Change Denied", MessageBoxButtons.OK)
                txtPassword.Focus()
                Exit Sub
            End If
            If txtFullName.Text <> "" Then
                MessageBox.Show("Inserting user into database...", "User", MessageBoxButtons.OK)

                Try
                    conn = New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Video Store\Video Store\DvDSystem.mdb")
                    conn.Open()
                    selectQuery = "SELECT userID FROM USERR WHERE userID = '" & txtUserID.Text & "'"
                    cmd = New OleDb.OleDbCommand(selectQuery, conn)
                    dr = cmd.ExecuteReader
                    'If successful Then
                    If dr.Read Then
                        MessageBox.Show("Name already exist!, Please enter different name.", "Data Entry Error", MessageBoxButtons.OK)
                        txtUserID.Focus()
                        'conn.Close()
                    Else
                        conn = New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Video Store\Video Store\DvDSystem.mdb")
                        conn.Open()
                        insertQuery = "INSERT INTO USERR VALUES('" & txtUserID.Text & "','" & txtPassword.Text & "','" & txtFullName.Text & "','" & cboProfile.Text & "')"
                        cmd = New OleDb.OleDbCommand(insertQuery, conn)
                        icount = cmd.ExecuteNonQuery
                        MessageBox.Show("Record successfully added", "User", MessageBoxButtons.OK, MessageBoxIcon.Information)
                        dbgUsers.Refresh()
                        'Clear the fields to allow the user to enter new fields.
                        txtPassword.Clear()
                        txtFullName.Clear()
                        With txtUserID
                            .Clear()
                            .Focus()
                            conn.Close()
                        End With
                    End If
                Catch ex As Exception
                End Try
            Else
                MessageBox.Show("Please enter user full name", "Data Entry Error", MessageBoxButtons.OK)
                txtFullName.Focus()
            End If
        Else
            MessageBox.Show("Please enter user password", "Data Entry Error", MessageBoxButtons.OK)
            txtPassword.Focus()
        End If
    Else
        MessageBox.Show("Please enter user id", "Data Entry Error", MessageBoxButtons.OK)
        txtUserID.Focus()
    End If

Add Timer, then call this code.
hope this help.

Public Sub loadRefresh()

        '///Auto reloading the data
        Dim dt As New DataTable
        Dim ds As New DataSet
        ds.Tables.Add(dt)
        Dim da As New MySqlDataAdapter("SELECT *FROM <table> ", conn)
        da.Fill(dt)
        dg_view1.DataSource = dt.DefaultView

    End Sub

how do i add timer? and also what does da stand for (da.fill) in the code

thnks.

ignore my previous comment, finally got it right just had to call the code from the form loading events.

thanks very much for all your efforts..really appreciate it.

I mentioned Timer coz i want my datagridview to automatically refresh for a certain time, with this i dont have to close/open my form or click a certain button to refresh it. well at least you have nailed it...cheers!

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.