Solved

Update Data Grid View when data updated in another form

Posted on 2014-10-23
7
1,590 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 32

Expert Comment

by:it_saige
Comment Utility
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
Comment Utility
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 32

Expert Comment

by:it_saige
Comment Utility
How are you binding to your dataset currently?

Code please.

-saige-
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:RadhaKrishnaKiJaya
Comment Utility
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 32

Accepted Solution

by:
it_saige earned 500 total points
Comment Utility
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
Comment Utility
0
 

Author Closing Comment

by:RadhaKrishnaKiJaya
Comment Utility
Thank u for your help.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now