Solved

Update Data Grid View when data updated in another form

Posted on 2014-10-23
7
1,725 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

932 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

12 Experts available now in Live!

Get 1:1 Help Now