Help - cant refresh rows in datagridview!

Im struggling to refresh my datagrid after updating its datasource from a different form. On Double click of the datagrid a new form is opened to edit the record, once the details in the form have been changed I need to get the datagrid to refresh and show the new detaisl(either a new row or changed record).

I know the database has been changed but it will not reflect in the datagrid(note I have tried datagrid.refresh), I have also tried calling the sub which populates the datagrid but with no luck. Any ideas



Answer this question

Help - cant refresh rows in datagridview!

  • Ben821217

    Hmm it still doesnt work, I think the problem is I cannot seem to access the instance of the original form correctly. I can see all the properties of the first form, but even if I try clsoe the first form from the second, it still stays open....
  • Paul Monaghan

    I had a similar problem in a web application because I was refreshing the dataview when in fact I needed to call a DataBind on the SQL datasource the grid was bound to for it's data.

  • Poolius

    OK the key here is to providing the child form with a reference to the first. The code example here simply uses a button to initate the call to the second form but this is in essence what you double click on the datagrid would be doing something similar.

    This shows a new property called baseform on the form2 which you can set when you instantiate the instance of the form. The tableadapter fill method is moved into a public method that I can then call from form2 because I now have a reference - so when I click on the button on form2 it will save the record to the database, call the PopulateDataset method on form1 and close the form.

    This shows a general principle but the concept is passing the reference of the base form to the child form - so you can call parent form methods or modify properties from the child.

    Public Class Form1

    ''' <summary>
    ''' Populate the Dataset
    ''' </summary>
    ''' <remarks></remarks>
    Sub PopulateDataset()
    '//Call your fill Code to populate the dataset here/.
    Me.Table1TableAdapter.Fill(Me.TestDataSet.Table1)
    End Sub

    ''' <summary>
    ''' Call the Populate dataset on loading form
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    PopulateDataset()
    End Sub

    ''' <summary>
    ''' Create a Child Form and ensure baseform property is set
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim x As New Form2
    x.BaseForm = Me '//Pass a reference from base form to Details form
    x.Show()
    End Sub


    Private Sub Table1BindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Table1BindingNavigatorSaveItem.Click
    Me.Validate()
    Me.Table1BindingSource.EndEdit()
    Me.Table1TableAdapter.Update(Me.TestDataSet.Table1)
    End Sub

    End Class

    Public Class Form2
    Dim _baseform As Form1


    ''' <summary>
    ''' A reference variable to the base form
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property BaseForm() As Form1
    Get
    Return Form1
    End Get
    Set(ByVal value As Form1)
    _baseform = value
    End Set
    End Property

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    '//Whatever database Save Code you have the database
    _baseform.TestDataSet.Table1.Rows(0).Item(0) = 99
    _baseform.Table1TableAdapter.Update(_baseform.TestDataSet)

    '//Can call method PopulateDataset in Form1 to repopulate
    '//the form1 dataset when you click the close button to close the form
    _baseform.PopulateDataset()
    Me.Close()
    End Sub

    End Class


  • J. Clark

    Hi,

    Thanks for the reply. Unfortunately this isnt working for me, ive managed to set the reference to the baseform ok however when I try and call my populate dataset code from within the child form I get the following error:

     

    System.NullReferenceException was unhandled
      Message="Object reference not set to an instance of an object."
     

    Any ideas what could be causing this

     

    EDIT - THIS IS NOW WORKING OK Didnt set the baseform property properly. Thankyou!


  • Help - cant refresh rows in datagridview!