Solved

Update Data Grid View when data updated in another form

Posted on 2014-10-23
7
2,038 Views
Last Modified: 2014-10-31
Hi Experts,
I have a form with data gridview which displays the result when I click "Search" button.  Then I select a row hit "Modify" button, that particular record popup in another form for change.  Once updated, when I go back, the main gridview is not auto updated.  Please help.  Thank you very much in advance.
0
Comment
Question by:RadhaKrishnaKiJaya
  • 3
  • 3
7 Comments
 
LVL 33

Expert Comment

by:it_saige
ID: 40400129
You need to update your datagrid's datasource.

One method is to rebind your datasource:
DataGridView1.DataSource = <whatever your datasource object is>

Open in new window

Another method is to use a CurrencyManager:
Dim cm as CurrencyManager(BindingContext(dg.DataSource), CurrencyManager)
cm.Refresh()

Open in new window


How are you binding to your datasource?

-saige-
0
 

Author Comment

by:RadhaKrishnaKiJaya
ID: 40400536
Thank u Saige for ur reply. But my problem is, currently I am in form2. My Datagridview is in form1. When I update the record in form2, my Datagridview in form1 should be updated. Thank u again.
0
 
LVL 33

Expert Comment

by:it_saige
ID: 40400549
How are you binding to your dataset currently?

Code please.

-saige-
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:RadhaKrishnaKiJaya
ID: 40400972
Private Sub cbSearch_Click(sender As Object, e As EventArgs) Handles pbSearch.Click
        Dim connection As New SqlConnection(sqlConnectionString)
        Dim validate_Panel As String = ""
        Dim cmd As New Data.SqlClient.SqlCommand
        Dim ds As New DataTable
        Dim bsource As New BindingSource
        Dim da As New Data.SqlClient.SqlDataAdapter
        Dim datatableMain As New System.Data.DataTable()
        Dim command As New SqlClient.SqlCommand
        searchResult = 1
        Dim commandText As String = "SELECT * from PNLSTROUTPRO"
        If commandText <> "" Then
            connection.Open()
            cmd = New SqlCommand(commandText, connection)
            da.SelectCommand = cmd
            da.Fill(ds)
            bsource.DataSource = ds
            dgvRetProc.DataSource = bsource
            With dgvRetProc
                .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
            End With

        End If
    End Sub
0
 
LVL 33

Accepted Solution

by:
it_saige earned 500 total points
ID: 40408403
The problem here, appears to be that you create new instances of the classes required to connect to your database along with a new binding source.  It should be easy enough to assign a public property to your second form that the first form can access when the second form returns a DialogResult - e.g.

Is some method that creates and calls the second form -
Dim searchForm As New Form2
If Form2.ShowDialog() = DialogResult.OK Then
	If Form2.IsDataModified Then
		Dim cm as CurrencyManager(BindingContext(dg.DataSource), CurrencyManager)
		cm.Refresh()
	End If
End If

Open in new window

In Form2 -
Public Class Form2
	Private _isDataModified As Boolean = False
	Public Property IsDataModified() As Boolean
		Get
			Return _isDataModified
		End Get
		Set
			If Not value.Equals(_isDataModified) Then
				_isDataModified = value
			End If
		End Set
	End Property

	' Blah blah filler code

	' Assuming that this method is in your second form
	Private Sub cbSearch_Click(sender As Object, e As EventArgs) Handles pbSearch.Click
		Dim connection As New SqlConnection(sqlConnectionString)
		Dim validate_Panel As String = ""
		Dim cmd As New Data.SqlClient.SqlCommand
		Dim ds As New DataTable
		Dim bsource As New BindingSource
		Dim da As New Data.SqlClient.SqlDataAdapter
		Dim datatableMain As New System.Data.DataTable()
		Dim command As New SqlClient.SqlCommand
		searchResult = 1
		Dim commandText As String = "SELECT * from PNLSTROUTPRO"
		If commandText <> "" Then
			connection.Open()
			cmd = New SqlCommand(commandText, connection)
			da.SelectCommand = cmd
			da.Fill(ds)
			bsource.DataSource = ds
			dgvRetProc.DataSource = bsource
			With dgvRetProc
				.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
			End With
		End If
	End Sub

	' Blah blah other methods and filler code

	'Some method that is called when you need to update your database.
	Public Sub DataModified()
		' Some code that does the updating and or inserting
		' You can also update _isDataModified based on an If statement or Try Catch
		_isDataModified = True
	End Sub
End Class

Open in new window


-saige-
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 40412952
0
 

Author Closing Comment

by:RadhaKrishnaKiJaya
ID: 40416323
Thank u for your help.
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question