Click here to Skip to main content
15,904,024 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
I have an update button and a datagridview that bind one of my table. when I click on update button it successfully update but my problem is my datagridview doesn't update automatically.Can you give me some idea or code to resolve my problem.

I'm a newbie in vb.net
tnx in advance
Posted
Updated 25-Apr-12 6:01am
v2

The DataGridView gets updated when the DataSource is modified when it is bound using the BindingSource. I have made a small code sample as below. To run the sample, create a Windows Forms application, double click on the Form1 in Design view to open the code file, replace the entire contents of the code file Form1.vb with the following code and run the application.

VB
Public Class Form1
    Dim DataGridView1 As New DataGridView()
    Dim ButtonUpdate As New Button()
    Dim BindingSource1 As New BindingSource()
    Dim DataTable1 As New DataTable()

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DataTable1.Columns.Add("Column1", GetType(System.String), Nothing)
        With DataTable1.Rows
            .Add("Row1")
            .Add("Row2")
            .Add("Row3")
        End With
        BindingSource1.DataSource = DataTable1
        DataGridView1.DataSource = BindingSource1

        ButtonUpdate.Dock = DockStyle.Bottom
        ButtonUpdate.Text = "Update"
        AddHandler ButtonUpdate.Click, AddressOf ButtonUpdate_Click
        Me.Controls.Add(ButtonUpdate)
        DataGridView1.Dock = DockStyle.Fill
        Me.Controls.Add(DataGridView1)
    End Sub
    Private Sub ButtonUpdate_Click(ByVal s As Object, ByVal e As EventArgs)
        DataTable1.Rows(0)(0) = "Row1 modified"
        DataTable1.Rows.Add("Row4")
        'By default the DataGridView gets updated. In case it is not updated then
        'the ResetBindings method can be used to make the DataGridView refresh 
        'the values from DataSource
        'BindingSource1.ResetBindings(False)
    End Sub
End Class

In case the DataGridView does not get updated then the ResetBindings method of BindingSource which is explained here BindingSource.ResetBindings Method[^] can be used.

I think this Code Project article
A Detailed Data Binding Tutorial[^]
may be helpful to understand the DataBinding concept.
 
Share this answer
 
v3
On update also rebind the datagrid with the source.
 
Share this answer
 
Just Bind the DataGridview on updation
 
Share this answer
 
Hi ,
Just make datasource empty and then Rebind again
Google on it
Here .[^]
Best Regards
M.Mitwalli
 
Share this answer
 
thanks. for the idea VJ Reddy and the link also help me.

But I made a subroutine that select all values in the table and call it in update button and it works.

thanks all for your replies...

Here is my code:

VB
Private Sub updatedgv()
        Dim conn As New MySqlConnection(My.Settings.myConn)
        Dim da As New MySqlDataAdapter
        Dim ds As New DataSet
        Dim str1 As String = "select * from tableName"
        da.SelectCommand = New MySqlCommand(str1, conn)
        da.Fill(ds)
        conn.Close()
        ProductDataGridView.DataSource = ds.Tables(0)
    End Sub
 
Share this answer
 
v3
Howdy 'all - really late reply here, but thought this might would help if someone was looking for a simple answer.
After you have made changes to your data execute these two lines:

VB
Dim cm As CurrencyManager = DirectCast(BindingContext(dg.DataSource), CurrencyManager)
cm.Refresh()
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900