PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2017 Updating Datagridview-VBForums
Results 1 to 15 of 15

Thread: Updating Datagridview

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2018
    Posts
    76

    Updating Datagridview

    Dear all,
    I have a Datagridview which is filled by an SQL DB table and that is displayed in Form1.
    In Form2 I'm adding some data to the DB table and I would like then data displayed in Form1 to get refreshed displaying then the new row.

    Therefore at the end of the INSERT SQL procedure in Form2, I wrote:

    Code:
    Form1.DtGrd1.DataSource = Nothing
    
            Form1.Tbl_CTOTableAdapter.Fill(Form1.CTO_DataSet.Tbl_CTO1)
           
            Form1.DtGrd1.Refresh()
    But nothing happens.

    What I'm doing wrong?

    Thanks,
    A.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,724

    Re: Updating Datagridview

    If you set the DataSource to Nothing then why would you expect the grid to show anything? Filling a DataTable is useless if that DataTable isn't bound to the grid that you want to display the data and that Refresh method is completely irrelevant, which reading the documentation would have indicated. It simply redraws a control and redrawing a control that is already drawn correctly can't help you get data into it.

    Regardless of anything else, you're going about this in the wrong way, which is quite common. Rather than have a form displaying all the data, open another form, the second form modifying the database, then regetting the data in the first form, you should be doing it the other way around. That means that any changes made on the second form should affect the DataTable that you already have first. The grid on your first form is bound to that DataTable so it will reflect those changes immediately. It's then the responsibility of the first form to save the changes from the DataTable back to the database using the same table adapter that it used to retrieve the data in the first place. Make the changes locally first and then push them back to the database in the same place as you got the data. Only one form has to know about the database. The second form is just about editing local data.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2018
    Posts
    76

    Re: Updating Datagridview

    Dear jmcilhinney,
    Thanks for your reply.
    I share your suggestion and initially I thought to design the application in the way you suggested. Unfortunately, data shown in the grid are not exactly the ones I’m saving in the db through the second form (e.g. I’m storing also a picture that is linked to the record in the table, making calculation among the fields). Therefore, I need to save the data first, and then feed them back in the grid.

    That’s why I’m looking for a way to have the data displayed in the grid ad this would have been refreshed. I understood I make a mistake in setting the datasource to nothing, but the idea was to set it to nothing and then reloading it, showing then the updated data. Any further suggestion in this direction is more than welcome.

    Thanks,
    A.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,724

    Re: Updating Datagridview

    I would still suggest doing it the other way around - there's nothing to stop the first form saving data that it's not displaying - but if you want to stick with the way you're doing it, are you closing the second form after saving the changes?

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 2018
    Posts
    76

    Re: Updating Datagridview

    Sure, the second form is closed after saving data, and - I don’t know if it is relevant for your suggestion- the form containing the datagridview is not closed (or hidden) while I’m loading/saving data in the second form.

    Thanks for your time,
    A.

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,724

    Re: Updating Datagridview

    In that case, have the first form do its own data retrieval after the second form closes. If you're calling ShowDialog then do it immediately after it returns. If you're calling Show then handle the FormClosed event.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Sep 2018
    Posts
    76

    Re: Updating Datagridview

    Great thank you, but now we are coming to the pain point, 'cause I exactly don't know how to retrieve the data. This is what I tried, but it doesn't work:

    Code:
    Private Sub BtnChiudi_Click(sender As Object, e As EventArgs) Handles BtnChiudi.Click
            Me.Close()
    
            Form1.Tbl_CTOTableAdapter.Fill(Form1.CTO_DataSet.Tbl_CTO1)
            Form1.DtGrd1.DataSource = Form1.TblCTOBindingSource
            GestioneGiocatori.DtGrd1.Refresh()
    
          
        End Sub
    What I'm doing wrong?

    A.

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,724

    Re: Updating Datagridview

    That's not doing it in the first form so you haven't actually tried anything yet. The code to retrieve the data should be in the form that is going to use the data. You already know how to retrieve the data if the form is already showing the data.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Sep 2018
    Posts
    76

    Re: Updating Datagridview

    Dear jmcilhinney,

    you're right - sorry I was in a rush and didn't read carefully your note.
    I've put this code - now I hope to have got your point:

    Code:
    Private Sub Form2_Closed(sender As Object, e As EventArgs) Handles Me.Closed
    
           
            Me.Tbl_CTOTableAdapter.Fill(Me.CTO_DataSet.Tbl_CTO)
            Me.DtGrd1.ResetBindings()
    
        End Sub
    but actually it doesn't work.
    I even tried several other possible solution, but nothing worked.
    I feel really frustrated...I cannot believe this is something not possible.

    Please help.
    Thanks,
    A.

  10. #10
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,724

    Re: Updating Datagridview

    Firstly, I said to handle the FormClosed event, not the Closed event. That's no a big deal though. What is a big deal is that you either have that code in the wrong form or you're handling the event of the wrong form. I told you to handle the FormClosed event of Form2 in Form1. The code you posted is either in Form2 or is handling the event of Form1. Neither is what I said.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Sep 2018
    Posts
    76

    Re: Updating Datagridview

    Dear jmcilhinney,
    sorry for that.

    So, I fixed the code inserting in the sub which is opening Form2 in Form1 the following statements:

    Code:
     Dim Form2 As New Form2
            AddHandler Form2.FormClosed, AddressOf Form2_FormClosed
    Then, in Form1:

    Code:
    Private Sub Form2_FormClosed(sender As Object, e As FormClosedEventArgs)
    
            Me.Tbl_CTOTableAdapter1.Fill(Me.CTO_DataSet.Tbl_CTO1)
            Me.Tbl_CTOTableAdapter2.Fill(Me.CTO_DataSet.Tbl_CTO2)
            'Me.DtGrd1.ResetBindings()
            'Me.DtGrd2.ResetBindings()
    
        End Sub
    This is working fine. Both the datagridviews get refreshed with new data in.

    But, now I'm getting an expection with the following code (that by the way we commented earlier in .
    This is throwing the exception "SystemNullReferenceException: object not set to an instance of an object. - System.Windows.Forms.DataGridView.CurrentRow.get returned Nothing" in the following line of code:

    Code:
     Filter = DtGrdGiocatori.CurrentRow.Cells(1).Value.ToString.Trim
    I'm almost getting convinced to adopt the "other way round" you were suggesting in one of your previous posts, but in case you have any suggestion to overcome this issue, please let me know.

    Forgive me in case I'm managing the two related threads in the wrong way.

    Thanks,
    A.

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Sep 2018
    Posts
    76

    Re: Updating Datagridview

    Dear jmcilhinney,
    I succeed to fix it! Just changed the handle event of the filter from CurrentCellChanged to CellClick and everything seems to work fine now.

    Please, master, do let me know in case you have any further comment and/or suggestion.
    If you could/would like, I'd like to deep dive a little bit other your proposed "other way-round" method. If possible and fine with you I will open a new thread on it and link it back to this so that you can see it.

    Just let me know.

    Thanks,
    A.

  13. #13
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,724

    Re: Updating Datagridview

    Quote Originally Posted by AndreB82 View Post
    I succeed to fix it! Just changed the handle event of the filter from CurrentCellChanged to CellClick and everything seems to work fine now.
    Or, if the issue was that CurrentRow was Nothing, you could have just added a test to see whether CurrentRow was Nothing.

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Sep 2018
    Posts
    76

    Re: Updating Datagridview

    I thought about it, but have not been able to figure out one...

  15. #15
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,724

    Re: Updating Datagridview

    If the issue is the fact that CurrentRow is Nothing then test whether CurrentRow is Nothing. That seems fairly obvious. Act only if it's not Nothing, or do something different if it is, e.g.
    vb.net Code:
    1. If DtGrdGiocatori.CurrentRow IsNot Nothing Then
    2.     'Set filter based on current row here.
    3. End If
    or:
    vb.net Code:
    1. If DtGrdGiocatori.CurrentRow Is Nothing Then
    2.     'Clear filter here.
    3. Else
    4.     'Set filter based on current row here.
    5. End If

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width