Click here to Skip to main content
11,500,106 members (64,252 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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 24-Apr-12 14:49pm
Edited 25-Apr-12 6:01am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.

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.
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

On update also rebind the datagrid with the source.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Just Bind the DataGridview on updation
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Hi ,
Just make datasource empty and then Rebind again
Google on it
Here .[^]
Best Regards
M.Mitwalli
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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:

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
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

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:

Dim cm As CurrencyManager = DirectCast(BindingContext(dg.DataSource), CurrencyManager)
cm.Refresh()
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 345
1 Maciej Los 290
2 DamithSL 230
3 Peter Leow 181
4 OriginalGriff 167
0 Sergey Alexandrovich Kryukov 335
1 Maciej Los 290
2 DamithSL 220
3 Peter Leow 181
4 OriginalGriff 167


Advertise | Privacy | Mobile
Web04 | 2.8.150520.1 | Last Updated 9 May 2014
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100