Question Refresh DataGridView

rkingmdu

Member
Joined
Dec 30, 2008
Messages
6
Programming Experience
3-5
Hi,

I have a Form(Form A) with a DataGridView Control. Users cannot add or edit any data here. I am fetching data from the DB, assign it to a Dataset and assign the dataset as the datasource for the DataGridView. I am doing this in the FormLoadEvent.

I have an ADD button in the same Form(Form A). When the ADD button is clicked, I open another form(Form B) and do the Insert there.

When I close Form B, the new row is not added to the DataGridView Contol in Form A. I am writing the same code to fetch all data from the DB, assign it to a dataset and assign the dataset as the datasource for the DataGridView. I have written this code in the Form_Closed Event of Form B.

Here's the code to refresh the Datagridview:
VB.NET:
Private Sub detail_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
     Dim prsmgr As New ProcessManager(_ConnectString)
        Dim ds As DataSet = prsmgr.GetAllEmployees()
        Dim Mainform As New frmMain
        'Mainform.DGRViewEmployer.DataBindings.Clear()
        Mainform.DGRViewEmployer.DataSource = Nothing
        'Mainform.DGRViewEmployer.DataSource = ds.Tables(0)
        Mainform.DGRViewEmployer.DataSource = ds.Tables(0)
End Sub
It seems like when Form B is closed, the dataset I am assigning to the DataGridView seems to have no effect on Form A.

When I close and open the Application, the new row shows up in the datagridview.

Any help would be appreciated.

Thanks
rkingmdu
 
Last edited by a moderator:
Consider this. You have a notebook and you write on the first page. You then get a second notebook that is exactly the same and you write something different on the first page. You now go back to the first notebook. What would you expect to see on its first page? The writing you originally put there or the writing you put in the other notebook? Obviously it's the writing you originally put there because any change you make to the second, new notebook will have no effect on the first.

The same goes for your application. Look at your code:
VB.NET:
Private Sub detail_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
     Dim prsmgr As New ProcessManager(_ConnectString)
        Dim ds As DataSet = prsmgr.GetAllEmployees()
        [B][U]Dim Mainform As [COLOR="Red"]New frmMain[/COLOR][/U][/B]
        'Mainform.DGRViewEmployer.DataBindings.Clear()
        Mainform.DGRViewEmployer.DataSource = Nothing
        'Mainform.DGRViewEmployer.DataSource = ds.Tables(0)
        Mainform.DGRViewEmployer.DataSource = ds.Tables(0)
End Sub
You're creating a new frmMain object and binding your new DataSet to that. That's not going to have any effect on the frmMain object that you already have displayed because they are two different objects. What you do to one has no effect on the other.

What you need to do is bind this new DataSet to the grid on the existing frmMain object, NOT a new one. The simplest way to do that is from frmMain to pass this dialogue a reference to itself when it creates it in the first place. You would need to write a property, method or constructor in the dialogue form to accept that reference and store it in a member variable so you can use it later.
 
Back
Top