Solved

Update Data Grid View when data updated in another form

Posted on 2014-10-23
7
2,233 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 34

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 34

Expert Comment

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

Code please.

-saige-
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 

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 34

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

Containers and Docker for Everyone

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
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.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

724 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