[RESOLVED] Refreshing a form-VBForums
Results 1 to 36 of 36

Thread: [RESOLVED] Refreshing a form

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    331

    Resolved [RESOLVED] Refreshing a form

    I often use sub Forms to do activities that I do not want to carry out on some Parent Form (if you will). Now for me a subform is just another form that I call a sub form because it is called from a different form that receives the information from the sub form. I understand that there are real sub forms in VB, but I have never used them, since what I am currently doing works just fine.

    So I sometimes call this sub form from the parent form and do whatever activity goes with the sub form. At that point I close the sub form and return to the parent form (the parent form may have been hidden or not). Usually, there is some data that some control in the parent form has received from the sub form. What generally occurs when I do this is that the data does not show in the parent form, unless I close the parent form and open it again.

    What I am after is a reliable means of refreshing the parent form after closing the sub form.

    I can tell that this might not be easily understood, and will attempt a better explanation if required.

  2. #2
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: Refreshing a form

    Hi,
    I think you may have to call on the tableadapter to fill it self again in order to load the new data
    I assume that you will be working in the same table but using diferent instances.

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    97,048

    Re: Refreshing a form

    Firstly, there's no such thing as a subform in VB.NET. A form is a form. The term you're looking for is probably "dialog" (or "dialogue" in English ).

    As for your issue, it's a common one. People often retrieve data from the database and display it in one form, open another form and makes changes directly to the data there, then wonder my the data they retrieved in the first form hasn't magically updated. The reason is because that data knows nothing about what's happening to the database behind its back. When you retrieve the data in the first place, you open a database connection, retrieve the data and then close the connection. Anything that then happens at the database has exactly zero effect on that data held in your app.

    The usual solution to this issue is to not do things backwards, i.e. rather than make changes to the database and then expect the first form to update based on that you actually make changes to the data in the first form first and then save those changes to the database from there. That means that the second form has no interaction with the database directly but rather only with data passed to it by the first form. The first form will then update based on your changes first and then it will call Update on the same data adapter or table adapter that you called Fill on to retrieve the data in the first place.

    If that's not appropriate for some reason then you need to do as suggested by Mike Storm and simply retrieve the data afresh. The reason that the data includes those changes after closing and reopening the form is exactly because you are querying the database to retrieve data after the changes are made. You can do that without closing the form or the application. You're probably doing it in the Load event handler of the form. Simply move the query code to a method and call that whenever appropriate, which would be from the Load event handler and also form a Button Click event handler or after your dialogue closes or wherever.
    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

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    331

    Re: Refreshing a form

    Mike,

    That is what I believed too. And no, I am working from two different tables. So what I did was run a fill query when I returned to what I have been calling the parent form.

    What I am actually doing is this. I have a datagridview in one table that is bound to Table A. So what I want to do (and it does this just fine) is populate a new row in that table, and consequently the bound DGV. So the data is to come from table B, also displayed in a DGV, in what I have been calling a sub form. in the sub form (as I apparently am mistakenly calling it) I use a double click event in the it's DGV to take the data in the row where the double click event occurs, then create a new row in Table A, followed by filling that new row with the data, then immediately save the new row. I can explain how this is done, but I am sure you know the methodology much better than I do. It works quite well, the data is added to Table A, from Table B without flaw. However, the parent form DGV does not reflect the change.

    The way I used to do this is that I had two containers on the same form, each with a DGV, one for Table A and one for Table B. I used the same process of transferring the data from Table B to a new row in Table A. When they were on the same form the changes were displayed in the form as soon as it happened. I just like the idea of using different forms, I feel it is much cleaner and that is just the way I roll right now.

  5. #5
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: Refreshing a form

    I will need to see the code, you using but from what you telling me, you either sending the data strait in to the database, or to the the binding source, if its going to the database directly, you have no option that i know of but to use the fill on the table adapter, is u sending it to the bindingsource of the parent form, you need to use Validate, and bindingsourse.EndEdit

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    331

    Re: Refreshing a form

    JM,

    I thought that I had read somewhere that there were sub forms in VB. Apparently I was wrong. But even if they existed I have no idea about them or how they work and the fact remains that I am fully aware that I am actually working with two forms and am just, for convenience sake, using the term parent form and sub form. Dialog may fit, but it doesn't sound right to me, but I will look it up some time and see. I will discontinue with parent form and subform and use Form A and Form B. Anyway, to the problem.

    Since I am using a dataset and table adapters there nothing at all that happens to the database, or the tables in it, until I end edit and update (OK, I probably should have stated that at the outset. Mea Culpa X 3). The data that I am getting from Table B, in what I have been calling the sub form is merely copied to a set of variables which are then used fill a new row in the Table A table adapter. Having said all of that I am considering, in the near future of working directly to the database instead of through a dataset and table adapters, but have not yet made the commitment for doing so.

    I am not entirely clear on what you are suggesting I do, but what I am getting out of it is that instead of carrying out the adding of the data to Table A from Form B, it is probably best to go back to form A and then add the data to Table A. That probably should have already occurred to me. I will check that out and get back to you guys.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    331

    Re: Refreshing a form

    OK Mike, here is what I have before I start messing around with stuff:

    Form A

    DGV data bound to table A (Approval signers table).

    Form B

    DGV data bound to Table B (Employee table)

    1. So the first action is to run the button event that sends me out to Form B

    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
    Me.Hide()
    subEmployeeList.Show()
    End Sub
    2. I then use a combobox to select a department and then query Table B to display only those employees in the selected department to display in the Table B DGV

    Private Sub cmbDepartments_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbDepartments.SelectedIndexChanged
    glbstrDept = cmbDepartments.Text
    Me.TblEmployeeTableAdapter.FillByDepartment(Me._MasterBase4_0ItemMasterDataSet.tblEmployee, glbstrDept)
    End Sub
    3. Since this is for change request approvers I can only pick one at a time, so I use a double click event in a row to select one of the employees from Table B, get the needed data and place it into a new row in table A

    Private Sub dgvEmployees_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvEmployees.CellDoubleClick
    'Add approver and fill in data to dgvApproverSign
    glbstrName = CStr(Me.dgvEmployees.Rows(e.RowIndex).Cells(0).Value.ToString)
    glbstrDept = CStr(Me.dgvEmployees.Rows(e.RowIndex).Cells(2).Value.ToString)
    glbstrLogin = CStr(Me.dgvEmployees.Rows(e.RowIndex).Cells(26).Value.ToString)
    glbstrPassword = CStr(Me.dgvEmployees.Rows(e.RowIndex).Cells(27).Value.ToString)
    glbstrJob = CStr(Me.dgvEmployees.Rows(e.RowIndex).Cells(1).Value.ToString)
    cmbDepartments.SelectedIndex = -1
    Dim newApproverRow As _MasterBase4_0ItemMasterDataSet.tblChangeApproveRow
    newApproverRow = _MasterBase4_0ItemMasterDataSet.tblChangeApprove.NewtblChangeApproveRow()
    newApproverRow.intChangeID = glbintCRNum
    newApproverRow.strName = glbstrName
    newApproverRow.strDept = glbstrDept
    newApproverRow.strLogin = glbstrLogin
    newApproverRow.strPassword = glbstrPassword
    newApproverRow.strJobTitle = glbstrJob
    newApproverRow.strMasterBaseType = glbstrBaseCategory
    _MasterBase4_0ItemMasterDataSet.tblChangeApprove.Rows.Add(newApproverRow)
    Me.Validate()
    Me.tblChangeApproveBindingSource.EndEdit()
    Me.TblChangeApproveTableAdapter.Update(Me._MasterBase4_0ItemMasterDataSet)
    Me.Close()
    frmSignRecord.Show()
    End Sub
    So now I am back at Form A, I know for a fact that there exists a new row in Table A with the desired data (I checked it every time I ran it). However, the DGV in Form one does not show the new row and that is where the problem lies. If I close form A and reopen it, it will then show the new data in it's DGV.

    What I am thinking now is that I should create the new row after I return to Form A, but that is looking to be a little bit tougher than I was thinking it would be.

  8. #8
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: Refreshing a form

    I'm not sure if there isnt a better sulution, but when i have to do something of that kind here is the code:


    Code:
     Private Sub TBFRNCRDataGridView_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles TBFRNCRDataGridView.CellDoubleClick
    
            Form1.TBCNTSBindingSource.AddNew()
            CType(Form1.TBCNTSBindingSource.Current, DataRowView).Item("Nome") = CType(TBFRNCRBindingSource.Current, DataRowView).Item("Nome")
            Form1.TBCNTSBindingSource.EndEdit()
    
        End Sub
    This is assuming, that table on the Form A has a primarykey, and that you only want part of the row in Form B.
    Anyway if the table does not has a Primarykey, it should.
    Last edited by Mike Storm; Sep 27th, 2017 at 10:59 PM.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    331

    Re: Refreshing a form

    That looks sweet Mike. I am going to take a break and get back to this and see how that works. But I bet that will do a lot better for me. When you do this I am assuming that when you go back to form 1 that the data will display in the DGV?

  10. #10
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: Refreshing a form

    Im transferingdata between Bindingsourses, so as soon as i call BindingSource.EndEdit data is shown

  11. #11
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: Refreshing a form

    As for the terms(names) used, JM is right, there is no subforms in vb, that term i think you may have read it for Access databases, i reamember that when i used access in a Master and Detail(Parent, child) they called the child a subform, but is not the same in VB, at best what u could call a subform would be a usercontrol added to a form but still not correct, but i m not the best person to explain you this, nor i consider to have enought knowloege to do so.

  12. #12
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    97,048

    Re: Refreshing a form

    Quote Originally Posted by gwboolean View Post
    I am not entirely clear on what you are suggesting I do, but what I am getting out of it is that instead of carrying out the adding of the data to Table A from Form B, it is probably best to go back to form A and then add the data to Table A. That probably should have already occurred to me. I will check that out and get back to you guys.
    You retrieve data from the database into DataTables in a DataSet on the first form. When you open the second form from the first, pass the data you already have to it, i.e. pass the relevant DataTable. Any changes you make to that DataTable in the second form will be automatically reflected in the first form because the first form is bound to that DataTable. When you want to save those changes, you do it in the first form by calling Update on your data adapter or table adapter, i.e. the same adapter that you called Fill on to retrieve the data in the first place. The second form doesn't even know that the database exists. All it knows about is the DataTable that is passed to it by its caller. As far as the first form is concerned, it doesn't differentiate between changes made by itself or changes made in the dialogue. They are all just changes made to a DataSet and it saves all those changes as appropriate.
    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

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

    Re: Refreshing a form

    Here's an example of the sort of thing I'm talking about:

    http://www.vbforums.com/showthread.p...ow-in-Dialogue

    In that example, the first form populates a DataTable and then a row of that DataTable gets modified in a dialogue. The dialogue makes no changes to the database. All it does is make changes to the DataRow that's passed to it. If those changes were to be saved then that would be done by the calling form after the dialogue closed.
    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

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    331

    Re: Refreshing a form

    Actually, I used to use sub forms in Access, so I know about them. However, I did think I had read something somewhere about sub forms in VB. However, that could be completely wrong, since I have never actually seen anything that even slightly resembles that.

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    331

    Re: Refreshing a form

    Mike,

    In your example above, in which form does the event occur. Is that in Form 1 or Form 2?

  16. #16
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: Refreshing a form

    Code:
    'This code goes under form 2, so i m trasfering data from form 2 to form 1
        Private Sub TBFRNCRDataGridView_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles TBFRNCRDataGridView.CellDoubleClick
            'Here i add a new row using the bindingsourse, i assume u have one on ur form
            Form1.TBCNTSBindingSource.AddNew()
            'Set form1 binding source field/column/item "Nome" to the same as in form2
            CType(Form1.TBCNTSBindingSource.Current, DataRowView).Item("Nome") = CType(TBFRNCRBindingSource.Current, DataRowView).Item("Nome")
            'This will commit changes, not update to the database
            Form1.TBCNTSBindingSource.EndEdit()
    
        End Sub

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    331

    Re: Refreshing a form

    OK, I already figured out which is form one so here is what I under stand and how I set up the code for one of the items to be worked.

    Me.tblChangeApproveBindingSource.AddNew()
    CType(Me.tblChangeApproveBindingSource.Current, DataRowView).Item("strName") = CType(TblEmployeeBindingSource.Current, DataRowView).Item("strFullName")
    At this point my only question is what is within the quotes. What I am guessing is that is the field name from the table it belongs to. Is that correct?

  18. #18
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: Refreshing a form

    Yes, the .Item(ColumnName).
    I just had a look in JMC sample, i think you should also considere have a look.
    He is passing data in the sence inverse to the one you need, but you can easly invert that, and he passes a datarow, in insted of a field, since you may want to pass more then 1 row, you may create a function based on jis sample to pass the row from form 2 to form 1, and then set each field value.

  19. #19
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: Refreshing a form

    By the way, a observation.... i dont know if u live the code as you post it here, and since i havent seen you using excepion handling, you should, some say i m over cautions, but alow that i dont alwys post my code here wraped in a Try Catch, in the actual project i do it in every Sub/Function/ and most of the events.


    Code:
        'This code goes under form 2, so i m trasfering data from form 2 to form 1
        Private Sub TBFRNCRDataGridView_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles TBFRNCRDataGridView.CellDoubleClick
    
            Try
    
                'Here i add a new row using the bindingsourse, i assume u have one on ur form
                Form1.TBCNTSBindingSource.AddNew()
                'Set form1 binding source field/column/item "Nome" to the same as in form2
                CType(Form1.TBCNTSBindingSource.Current, DataRowView).Item("Nome") = CType(TBFRNCRBindingSource.Current, DataRowView).Item("Nome")
                'This will commit changes, not update to the database
                Form1.TBCNTSBindingSource.EndEdit()
    
            Catch ex As Exception
                MessageBox.Show(ex.Message & vbNewLine & ex.StackTrace, "Error!!!", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
    
        End Sub

  20. #20

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    331

    Re: Refreshing a form

    I do use exception handling. In fact, I way over use it. I just do not include it with code that I am posting as I feel like it gets in the way and might confuse things.

    OK, so here is where I am at. I replaced the method I was using (see above) and replaced it with this:

    Me.tblChangeApproveBindingSource.AddNew()
    CType(Me.tblChangeApproveBindingSource.Current, DataRowView).Item("intChangeID") = glbintCRNum
    CType(Me.tblChangeApproveBindingSource.Current, DataRowView).Item("strName") = CType(TblEmployeeBindingSource.Current, DataRowView).Item("strFullName")
    CType(Me.tblChangeApproveBindingSource.Current, DataRowView).Item("strDept") = CType(TblEmployeeBindingSource.Current, DataRowView).Item("strDepartment")
    CType(Me.tblChangeApproveBindingSource.Current, DataRowView).Item("strLogin") = CType(TblEmployeeBindingSource.Current, DataRowView).Item("strLogin")
    CType(Me.tblChangeApproveBindingSource.Current, DataRowView).Item("strPassword") = CType(TblEmployeeBindingSource.Current, DataRowView).Item("strPassword")
    CType(Me.tblChangeApproveBindingSource.Current, DataRowView).Item("strJobTitle") = CType(TblEmployeeBindingSource.Current, DataRowView).Item("strJobTitle")
    CType(Me.tblChangeApproveBindingSource.Current, DataRowView).Item("strMasterBaseType") = glbstrBaseCategory
    dgvEmployees.Enabled = False
    Me.Validate()
    Me.tblChangeApproveBindingSource.EndEdit()
    Me.TblChangeApproveTableAdapter.Update(Me._MasterBase4_0ItemMasterDataSet)
    Me.TblChangeApproveTableAdapter.FillByChangeID(Me._MasterBase4_0ItemMasterDataSet.tblChangeApprove, glbintCRNum)
    Me.Close()
    It does exactly what the replaced code does and I like that it is just a lot cleaner than what I was doing. However, when I close this form (Form B) and return to Form A, the DGV still does not display in the Form A DGV.

    I have been looking at JMs example. I think I understand most of it, there are a couple of things I would still need to understand so am still going through it. There is one thing though. It appears to me that this is done in the load event (can it be done in another event or routine?). It would seem that if this is in the load event, and since everything that I am doing is after the load event (in both forms) then how would I be able to apply that?

  21. #21
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: Refreshing a form

    since u are transfering from FormB to FormA

    Code:
             'Here you have "Me"
    CType(FormA.tblChangeApproveBindingSource.Current, DataRowView).Item("strName") = CType(TblEmployeeBindingSource.Current, DataRowView).Item("strFullName")

    The code goes under form B, that contains the data you want to transfer to A

    so Ctype(FormA) = FormB

  22. #22
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: Refreshing a form

    Code:
    'Me = Form B
    CType(FormA.tblChangeApproveBindingSource.Current, DataRowView).Item("strName") = CType(Me.TblEmployeeBindingSource.Current, DataRowView).Item("strFullName")

  23. #23

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    331

    Re: Refreshing a form

    Not sure I get that. Form A is with Table A and I will call its DGV, DGV A.

    1. I start in Form A and DGV A displays any existing approvers that are already there (In this case there are none already there, but there could be). In this case what we have is a brand new item/part that is going to be introduced. Let me illustrate:

    Name:  ApproverList.jpg
Views: 50
Size:  22.9 KB

    Since this a newly created part it has no approvers listed yet.

    2. I click the Add Approvers Button

    Code:
        Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
            subEmployeeList.Show()
            Me.TblChangeApproveTableAdapter.FillByChangeID(Me._MasterBase4_0ItemMasterDataSet.tblChangeApprove, glbintCRNum)
        End Sub
    Form B is now open

    Name:  EmployeeListOne.jpg
Views: 44
Size:  16.0 KB

    3. I then select the operations department from the combobox and now DGV B in form B displays all the employees for the Operations department (there is only one)

    [Code]
    Private Sub cmbDepartments_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbDepartments.SelectedIndexChanged
    With lblInfo
    .Visible = True
    .TextAlign = ContentAlignment.MiddleLeft
    .Text = "From the list below, double click" +
    vbNewLine +
    "a name in the list to add them to the" +
    vbNewLine +
    "Approver list."
    End With
    glbstrDept = cmbDepartments.Text
    Me.TblEmployeeTableAdapter.FillByDepartment(Me._MasterBase4_0ItemMasterDataSet.tblEmployee, glbstrDept)
    End Sub
    [Code]

    Name:  EmployeeListTwo.jpg
Views: 44
Size:  19.2 KB

    4. I double click in any row to select that employee (easy since there is only one).

    [Code]
    Private Sub dgvEmployees_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvEmployees.CellDoubleClick
    Me.tblChangeApproveBindingSource.AddNew()
    CType(Me.tblChangeApproveBindingSource.Current, DataRowView).Item("intChangeID") = glbintCRNum
    CType(Me.tblChangeApproveBindingSource.Current, DataRowView).Item("strName") = CType(TblEmployeeBindingSource.Current, DataRowView).Item("strFullName")
    CType(Me.tblChangeApproveBindingSource.Current, DataRowView).Item("strDept") = CType(TblEmployeeBindingSource.Current, DataRowView).Item("strDepartment")
    CType(Me.tblChangeApproveBindingSource.Current, DataRowView).Item("strLogin") = CType(TblEmployeeBindingSource.Current, DataRowView).Item("strLogin")
    CType(Me.tblChangeApproveBindingSource.Current, DataRowView).Item("strPassword") = CType(TblEmployeeBindingSource.Current, DataRowView).Item("strPassword")
    CType(Me.tblChangeApproveBindingSource.Current, DataRowView).Item("strJobTitle") = CType(TblEmployeeBindingSource.Current, DataRowView).Item("strJobTitle")
    CType(Me.tblChangeApproveBindingSource.Current, DataRowView).Item("strMasterBaseType") = glbstrBaseCategory
    dgvEmployees.Enabled = False
    Me.Validate()
    Me.tblChangeApproveBindingSource.EndEdit()
    Me.TblChangeApproveTableAdapter.Update(Me._MasterBase4_0ItemMasterDataSet)
    Me.TblChangeApproveTableAdapter.FillByChangeID(Me._MasterBase4_0ItemMasterDataSet.tblChangeApprove, glbintCRNum)
    Me.Close()
    End Sub

    [Code]

    Since this closed form B, I am back to Form A (metaphorically, since it was never gone or hidden)

    Name:  ApproverList.jpg
Views: 50
Size:  22.9 KB

    As you can see DGV A does not display the new record. I checked the table and it is indeed all there.

    Which brings me back to why the Me. is where it is. The data transaction all occurs in Form B, since that is where the Employee table resides. So am I understanding what you are talking about?

  24. #24
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: Refreshing a form

    i supouse tblChangeApproveBindingSource is on form A and TblEmployeeBindingSource is on form B?

  25. #25
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: Refreshing a form

    so if i m understanding correctly i FormA.TblEmployeeBindingSource NOT Me.TblEmployeeBindingSource.
    And by the way remove the ME from there, it has no use in that situation.
    The code you showing me seems like you have bouth bindingsources in the same Form.

  26. #26
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: Refreshing a form

    Lets see if like this its easyer:

    My parent form = Form A
    Code:
    Public Class Parent_Form
        'This the form you hide
        Private Sub TBCNTSBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles TBCNTSBindingNavigatorSaveItem.Click
    
            UPDATEPARENT()
    
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'TODO: This line of code loads data into the 'DBaseDataSet.TBCNTS' table. You can move, or remove it, as needed.
            TBCNTSTableAdapter.Fill(DBaseDataSet.TBCNTS)
    
        End Sub
    
        Private Sub ToolStripButton1_Click(sender As Object, e As EventArgs) Handles ToolStripButton1.Click
    
            Dim ChildFrm As New Child_Form(TBCNTSBindingSource)
            ChildFrm.Show()
    
        End Sub
    
        Friend Sub UPDATEPARENT()
    
            Validate()
            TBCNTSBindingSource.EndEdit()
            TableAdapterManager.UpdateAll(DBaseDataSet)
    
        End Sub
    End Class

    My child form = form B
    Code:
    Public Class Child_Form
    
    
        Private Parent_BindingSourse As BindingSource
    
        Friend Sub New(Parent_BSourse As BindingSource)
    
    
            ' This call is required by the designer.
            InitializeComponent()
    
            ' Add any initialization after the InitializeComponent() call.
    
            'Like this you access directly to the parentform bindingsource
            Parent_BindingSourse = Parent_BSourse
    
        End Sub
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'TODO: This line of code loads data into the 'DBaseDataSet.TBFRNCR' table. You can move, or remove it, as needed.
            TBFRNCRTableAdapter.Fill(DBaseDataSet.TBFRNCR)
    
            'Adds a CheckBoxColum to select multiple rows, this column will apear after the bounded columns
            Dim CL As New DataGridViewCheckBoxColumn With {
                .Name = "CL_SELECT", .HeaderText = "Select", .Width = 75}
            TBFRNCRDataGridView.Columns.Add(CL)
    
        End Sub
    
    
        Sub TRANSFER_ROWS()
    
    
            Try
    
                Validate()
                'Selects only the checked rows in the datagridview
                Dim DGVROWS = (From RS In TBFRNCRDataGridView.Rows.Cast(Of DataGridViewRow) Where CBool(RS.Cells("CL_SELECT").Value) = True)
    
                'Transferes the data in to the parentform datagridview
                For Each R In DGVROWS
    
                    Parent_BindingSourse.AddNew()
                    CType(Parent_BindingSourse.Current, DataRowView).Item("Nome") = R.Cells(1).Value
                    'Duplicate the line above for each column in you table
    
                Next
    
                Parent_Form.UPDATEPARENT()
    
            Catch ex As Exception
                MessageBox.Show(ex.Message & vbNewLine & ex.StackTrace, "Error!!!", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
    
        End Sub
    
        Private Sub TransferDataButton_Click(sender As Object, e As EventArgs) Handles TransferDataButton.Click
    
            TRANSFER_ROWS()
    
        End Sub
    
    
    End Class
    This is diferent from what i showed you first, i was waiting for you to understand the first piece to show this.

    PS: In this code, instede of using the cell click event i used a button, and added also a extra Checkbox column in to the datagrid in Form B, alowing to select multiple rows by checking that column, then on button click it will query the datagridview rows that are checked and tranfer them in to the bindingsource in Form A, parent form.
    Last edited by Mike Storm; Sep 28th, 2017 at 02:00 AM.

  27. #27

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    331

    Re: Refreshing a form

    It looks to me like the primary change in this is that you save the data in Form A after the transaction is complete in form B. Is that correct? Since I have the BindingSources for both tables in both Forms I can see how that can be done. Am I correct?

  28. #28
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: Refreshing a form

    Yes, im preparing a project, will upload the code, give me 5 minutes.

  29. #29

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    331

    Re: Refreshing a form

    Cool. However, after looking at that I am going to finish my whiskey and call it a night and then see if I can finish this up in the morning. You do keep late hours Mike.

  30. #30
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: Refreshing a form

    Lol, yeah, sometimes.

  31. #31
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: Refreshing a form

    Create a project, add 2 forms
    Name the,
    First: Parent_Form
    Second: Child_Form

    Add a datagridview and a button to each form
    Name the datagridview,
    On the Parent form: ParentDGV
    On the child form: ChildDGV

    Parent form code

    Code:
    Public Class Parent_Form
    
        Private ParentTable As New DataTable("Parent")
        Private ParentBindingSource As New BindingSource
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            With ParentTable
    
                .Columns.Add("ID", GetType(Integer)).AutoIncrement = True
                .Columns.Add("Name", GetType(String))
    
            End With
    
            ParentBindingSource.DataSource = ParentTable
            ParentDGV.DataSource = ParentBindingSource
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim frm As New Child_Form(ParentBindingSource)
            frm.Show()
        End Sub
    End Class
    Child form:

    Code:
    Public Class Child_Form
    
        Private Parent_BSourse As BindingSource
        Private Child_Table As DataTable
        Private Child_BindingSource As New BindingSource
    
        Friend Sub New(P_BSourse As BindingSource)
    
    
            ' This call is required by the designer.
            InitializeComponent()
    
            ' Add any initialization after the InitializeComponent() call.
    
            'Like this you access directly to the parentform bindingsource
            Parent_BSourse = P_BSourse
    
            Child_Table = New DataTable("Parent")
    
            With Child_Table
    
                .Columns.Add("ID", GetType(Integer)).AutoIncrement = True
                .Columns.Add("Name", GetType(String))
    
                .Rows.Add(Nothing, "Mike")
                .Rows.Add(Nothing, "Alice")
                .Rows.Add(Nothing, "John")
                .Rows.Add(Nothing, "Some")
                .Rows.Add(Nothing, "One")
                .Rows.Add(Nothing, "Else")
    
            End With
    
            Child_BindingSource.DataSource = Child_Table
    
            ChildDGV.DataSource = Child_BindingSource
    
        End Sub
    
        Private Sub Child_Form_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
    
            'Adds a CheckBoxColum to select multiple rows, this column will apear after the bounded columns
            Dim CL As New DataGridViewCheckBoxColumn With {
                .Name = "CL_SELECT", .HeaderText = "Select", .Width = 75}
            ChildDGV.Columns.Add(CL)
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            TRANSFER_ROWS()
    
        End Sub
    
        Sub TRANSFER_ROWS()
    
    
            Try
    
                Validate()
                'Selects only the checked rows in the datagridview
                Dim DGVROWS = (From RS In ChildDGV.Rows.Cast(Of DataGridViewRow) Where CBool(RS.Cells("CL_SELECT").Value) = True)
    
                'Transferes the data in to the parentform datagridview
                For Each R In DGVROWS
    
                    Parent_BSourse.AddNew()
                    CType(Parent_BSourse.Current, DataRowView).Item("Name") = R.Cells(1).Value
                    'Duplicate the line above for each column in you table
    
                Next
    
                'Here goes the update for the parent as u seen in the code on the previus post
                'Parent_Form.UPDATEPARENT()
    
            Catch ex As Exception
                MessageBox.Show(ex.Message & vbNewLine & ex.StackTrace, "Error!!!", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
    
        End Sub
    End Class

  32. #32

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    331

    Re: Refreshing a form

    I have been playing with this a little bit and I can get the refresh (the record shows in the DGV) by running the fill query after I get back Form A. So the question, since the add button is complete, how can I put the query into another routine and auto-run that routine after I get back to Form A? I really hate the idea of having to do anything, like using a button, to run the query. It just doesn't seem right to have to have to do anything after the data transaction is complete and I am back to form A.

  33. #33

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    331

    Re: Refreshing a form

    OK, I think I see where you are going and how it will get me there. However, the whiskey is done, my wife won't let me have another so I am bed bound and will work through it tomorrow. Thanks Mike. And do something about that insomnia.

  34. #34
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: Refreshing a form

    lol
    You welcome, i do spend alot of time in from of the computer...
    Thats what happens when you divorce and dont have a "Madame" moning she alone in bed.

  35. #35
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    31,256

    Re: Refreshing a form

    Quote Originally Posted by gwboolean View Post
    I have been playing with this a little bit and I can get the refresh (the record shows in the DGV) by running the fill query after I get back Form A. So the question, since the add button is complete, how can I put the query into another routine and auto-run that routine after I get back to Form A? I really hate the idea of having to do anything, like using a button, to run the query. It just doesn't seem right to have to have to do anything after the data transaction is complete and I am back to form A.
    If you can put your requery code into a new sub, then use .ShowDialog when showing Form B... then right after that, call the sub that does the refreshing.... that should do it.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  36. #36

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    331

    Re: Refreshing a form

    Wow, that was a fine thing first thing in the morning!

    Tech, that works. It is the same thing that JM was talking about previously. It is also obvious I should have read through the code he suggested a lot more closely. Sorry JM, but what you had was right, just hard for me to get through. I looked at the information I have on that, but am still not sure what it is supposed to do. So exactly what is going on with this?

Tags for this Thread

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

Survey posted by VBForums.