Results 1 to 8 of 8

Thread: [2008] update Datagridview after editing details in edit form

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2008
    Posts
    15

    [2008] update Datagridview after editing details in edit form

    Hi

    I have a datagridview that when I double click a record it opens a second form where i can edit the details. Upon saving and closing this second form the datagridview in the first form is not refreshed or updated. I know that the data is being saved becase if i close the datagridview form and reopen it again, the correct data appears.

    Code:
     Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    
    
            Me.Validate()
            Me.ClientesBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.Bd1DataSet)
    
    'Presumably i need to call something here to update the datagrid view in my first form like refresh or refill or update???'
    
            Me.Close()
    
    
        End Sub
    Both forms are connected to ClientesBindingSource.

    Any help much appreciated.

  2. #2
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424

    Re: [2008] update Datagridview after editing details in edit form

    You need to re-populate the data in the GridView of the first form after you close the second form. Here is some code which will give you an idea about that.

    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button2_Click( _
    4.         ByVal sender As System.Object, _
    5.         ByVal e As System.EventArgs _
    6.     ) Handles Button2.Click
    7.  
    8.         Dim frm3 As New Form3
    9.         If frm3.ShowDialog = Windows.Forms.DialogResult.OK Then
    10.             PopulateData()
    11.         End If
    12.         frm3.Dispose()
    13.     End Sub
    14.  
    15.     Private Sub PopulateData()
    16.         ' Code to populate the data
    17.     End Sub
    18.  
    19. End Class

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2008
    Posts
    15

    Re: [2008] update Datagridview after editing details in edit form

    thanks Deepak

    i tried to call the populatedata command, but it didn't seem to do anything. I have been checking for solutions to this all over the internet for days now. i have found several posts but very few people who seem to have resolved what seems to me should be a very simple problem.

    any more help/suggestions much appreciated.

    thanks

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] update Datagridview after editing details in edit form

    The dialogue shouldn't be saving anything. If the data needs to be saved immediately then it should be done by the caller after the dialogue has closed. There's a link in my signature about updating a grid row in a dialogue. After the dialogue closes you could call Update on your adapter if required.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2008
    Posts
    15

    Post Re: [2008] update Datagridview after editing details in edit form

    Hi

    thanks for your link jmcilhinney, i have now managed to get this working using the following code in my datagridview form

    Code:
    Public Class Form2
    
        Private Sub Form2_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
            Me.Validate()
            Me.ClientesBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.Bd1DataSet)
        End Sub
    
        Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'TODO: This line of code loads data into the 'Bd1DataSet.Clientes' table. You can move, or remove it, as needed.
            Me.ClientesTableAdapter.Fill(Me.Bd1DataSet.Clientes)
    
        End Sub
    
    
        ' Handle the BindingComplete event to ensure the two forms
        ' remain synchronized.
        Private Sub bindingSource1_BindingComplete(ByVal sender As Object, _
            ByVal e As BindingCompleteEventArgs) Handles ClientesBindingSource.BindingComplete
            If e.BindingCompleteContext = BindingCompleteContext.DataSourceUpdate _
                AndAlso e.Exception Is Nothing Then
                e.Binding.BindingManagerBase.EndCurrentEdit()
            End If
    
        End Sub
    
    
        Private Sub ClientesDataGridView_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles ClientesDataGridView.CellDoubleClick
    
            Dim frm4 As New Form4
            frm4.DataSource = Me.ClientesBindingSource(e.RowIndex)
            frm4.ShowDialog()
    
        End Sub
    
    End Class

    and in my edit form

    Code:
    Public Class Form4
        Public Property DataSource() As Object
            Get
                Return Me.ClientesBindingSource.DataSource
            End Get
            Set(ByVal value As Object)
                Me.ClientesBindingSource.DataSource = value
            End Set
        End Property
        Private Sub ClientesBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClientesBindingNavigatorSaveItem.Click
            Me.Validate()
            Me.ClientesBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.Bd1DataSet)
    
        End Sub
    
        Private Sub btntosave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btntosave.Click
            
    'this works exactly the same calling only  Me.ClientesBindingSource.EndEdit()
    Me.Validate()
            Me.ClientesBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.Bd1DataSet)
            Me.Close()
        End Sub
    End Class
    however, the binding navigator fails to work with this method.

    what i am really trying to do is make a couple of forms like those in Microsoft Office Accounting 2008. In this program you have the datagrid style form to see the customers and then a details form for editing them. upon closing the edit form the details are immediately updated in the datagrid view form (i seem to have got this part working).

    however, the edit form can also be opened independently of the datagridview form, simply by clicking add new customer on the main form of the program. using my method i wouldn't be able to save details of any new customer unless the datagridview form was open as the details are only getting saved on closing this form.

    is there another way to do this?

  6. #6

    Thread Starter
    New Member
    Join Date
    Sep 2008
    Posts
    15

    Re: [2008] update Datagridview after editing details in edit form

    Another question!!!!

    Feeling so happy today that i have got this thing working, but now i am struggling to addnew, movenext, moveprevious without the use of my bindingnavigator.

    Presumably i would need to do something with this code for next/previous

    Code:
    Public Class Form4
        Public Property DataSource() As Object
            Get
                Return Me.ClientesBindingSource.DataSource
            End Get
            Set(ByVal value As Object)
                Me.ClientesBindingSource.DataSource = value
            End Set

    adding a new row is even more daunting.

    It feels like every problem solved generates 100 more!

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] update Datagridview after editing details in edit form

    All the BindingNavigator does is call methods of the associated BindingSource. You can do the same in code. All the navigation methods are self-explanatory. To delete the current record you call RemoveCurrent. AddNew will create a new record and return it as a DataRowView. You can set the fields of that DataRowView by numeric index or column name. Once you've set the fields you call EndEdit to add the row to the underlying DataTable.

    You'll note that in that thread of mine, to edit a row I get the BindingSource's Current property as a DataRowView, get the corresponding DataRow from that and pass it to the dialogue. You can do the same with the result of AddNew. If the user clicks OK then you call EndEdit to commit. If they press Cancel then you call CancelEdit to discard.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    New Member
    Join Date
    Sep 2008
    Posts
    15

    Re: [2008] update Datagridview after editing details in edit form

    Hi

    thanks jmcilhinney. I don't know if was clear in my previous post. I appreciate that while it is possible to call the methods of the bindingsource to AddNew etc. what i am struggling to do is make this happen in my edit details form.

    For example, to try and move to the next record in my details form i tried calling

    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Me.ClientesBindingSource.DataSource.MoveNext()
    
    
        End Sub
    However i was returned the error

    [COLOR="Red"]Public member 'MoveNext' on type 'DataRowView' not found

    I guess this is becuase my details form is using a DataRowView from my main form.

    any suggestions?

    thanks for your help as always

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width