?
Solved

Update Data Grid View when data updated in another form

Posted on 2014-10-23
7
Medium Priority
?
2,462 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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 

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 2000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

762 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