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

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

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 Update ID's in bound DGV-VBForums
Results 1 to 8 of 8

Thread: Update ID's in bound DGV

  1. #1

    Thread Starter
    Fanatic Member schoemr's Avatar
    Join Date
    Apr 2016
    Location
    South Africa
    Posts
    940

    Update ID's in bound DGV

    Hi,

    I have two related tables (one to many) tblUsers & tblTaskContext - for every user that is selected the first DGV, the 2nd DGV display tasks for that person.

    In this example User 7 have completed 6 tasks.

    Name:  Capture1.JPG
Views: 126
Size:  35.7 KB

    No problem.

    Next I want to 'transfer' tasks from one UserID to another UserID. So I added a unbound column to the bottom DGV ("Column1") and make it checkbox.

    Now I want to select all tasks for User#7 and transfer that to User#4 (I specify this user manually - (for now) by means of a manual input into a textbox.

    Name:  Capture2.JPG
Views: 118
Size:  36.9 KB

    Now I press the button once:

    Name:  Capture3.JPG
Views: 118
Size:  32.6 KB

    Every selected row did not change. I press button again:

    Name:  Capture4.JPG
Views: 117
Size:  28.7 KB

    Still....one record remains and I press button again...

    Then I think....maybe I should suspend the DGV binding until every row is completed. But also this does not work.

    Code:
    TblTaskContextBindingSource2.SuspendBinding()
    
            For Each row As DataGridViewRow In Me.TblTaskContextDataGridView.Rows
                Dim checked As Boolean = CType(row.Cells("Column1").Value, Boolean)
                If checked Then
                    row.Cells(2).Value = UserIDTextBox1.Text
                End If
            Next
    
            TblTaskContextBindingSource2.ResumeBinding()
    What am i do wrong please?
    Don't miss the whole point of the dance...

    https://www.youtube.com/watch?v=qHnIJeE3LAI

  2. #2
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,004

    Re: Update ID's in bound DGV

    Call Validate() before going through row loop, or you could commit the changes to the checkbox col as you are changing them through the CurrentCellDirtyStateChanged event

    Code:
        Private SubDataGridView1_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles DataGridView1.CurrentCellDirtyStateChanged
            If DataGridView1.IsCurrentCellDirty Then
                DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
            End If
        End Sub

  3. #3

    Thread Starter
    Fanatic Member schoemr's Avatar
    Join Date
    Apr 2016
    Location
    South Africa
    Posts
    940

    Re: Update ID's in bound DGV

    Quote Originally Posted by kpmc View Post
    Call Validate() before going through row loop, or you could commit the changes to the checkbox col as you are changing them through the CurrentCellDirtyStateChanged event

    Code:
        Private SubDataGridView1_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles DataGridView1.CurrentCellDirtyStateChanged
            If DataGridView1.IsCurrentCellDirty Then
                DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
            End If
        End Sub
    Hi kpmc

    It is still doing the same thing....
    Don't miss the whole point of the dance...

    https://www.youtube.com/watch?v=qHnIJeE3LAI

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

    Re: Update ID's in bound DGV

    Whenever working with bound data, work with the bound data, not the UI. Get the bound items from the grid rows of interest based on the unbound column, then use those items, e.g.
    vb.net Code:
    1. Dim rows = Me.TblTaskContextDataGridView.Rows.
    2.                                          Cast(Of DataGridViewRow)().
    3.                                          Where(Function(dgvr) CBool(dgvr.Cells("Column1").Value)).
    4.                                          Select(Function(dgvr) dgvr.DataBoundItem).
    5.                                          Cast(Of DataRow)().
    6.                                          ToArray()
    7.  
    8. For Each row In rows
    9.     '...
    10. Next
    By creating the array first and then looping through that, you avoid the issue where changes you make within the loop have an affect on the list you're looping through.

  5. #5
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,004

    Re: Update ID's in bound DGV

    Quote Originally Posted by schoemr View Post
    Hi kpmc

    It is still doing the same thing....
    I always add a bool column to the source datatable and set/get values from it and leave the dgv for display purpose only.

    There is no reason to suspend binding.

    Code:
            Dim SelRows() As DataRow = Dt.Select("Column1=True")
    
            For Each Dr As DataRow In SelRows
                Dr("ColumnToUpdate") = UserIDTextBox1.Text
            Next
    Which is kind of the direction jmc is pointing you.

  6. #6

    Thread Starter
    Fanatic Member schoemr's Avatar
    Join Date
    Apr 2016
    Location
    South Africa
    Posts
    940

    Re: Update ID's in bound DGV

    Quote Originally Posted by jmcilhinney View Post
    Whenever working with bound data, work with the bound data, not the UI. Get the bound items from the grid rows of interest based on the unbound column, then use those items, e.g.
    vb.net Code:
    1. Dim rows = Me.TblTaskContextDataGridView.Rows.
    2.                                          Cast(Of DataGridViewRow)().
    3.                                          Where(Function(dgvr) CBool(dgvr.Cells("Column1").Value)).
    4.                                          Select(Function(dgvr) dgvr.DataBoundItem).
    5.                                          Cast(Of DataRow)().
    6.                                          ToArray()
    7.  
    8. For Each row In rows
    9.     '...
    10. Next
    By creating the array first and then looping through that, you avoid the issue where changes you make within the loop have an affect on the list you're looping through.
    Hi John,

    I am getting this error:
    Code:
    Unable to cast object of type 'System.Data.DataRowView' to type 'System.Data.DataRow
    Don't miss the whole point of the dance...

    https://www.youtube.com/watch?v=qHnIJeE3LAI

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

    Re: Update ID's in bound DGV

    Apologies. You can change this:
    vb.net Code:
    1. Cast(Of DataRow)().
    to this:
    vb.net Code:
    1. Cast(Of DataRowView)().
    and then you'll get a DataRowView array instead. If you just want to change a field value then using a DataRowView is the same as using a DataRow. If you want to use the actual DataRows then change this:
    vb.net Code:
    1. Select(Function(dgvr) dgvr.DataBoundItem).
    2. Cast(Of DataRow)().
    to this:
    vb.net Code:
    1. Select(Function(dgvr) DirectCast(dgvr.DataBoundItem, DataRowView).Row).

  8. #8

    Thread Starter
    Fanatic Member schoemr's Avatar
    Join Date
    Apr 2016
    Location
    South Africa
    Posts
    940

    Re: Update ID's in bound DGV

    Hi John,

    I have to apologize as I am the one needing help and I am the one not understanding

    To update cells that is not an ID is working perfect (I can update "NameOfTsk" all at once)

    Because these are two related tables and I am try to update an ID field what happens is that the moment a row ID is update the UI responds to reflect that change.

    I think what should happen is the loop must finish and then only the UI must reflect that changes.

    I have try many methods from what I research.. no success. From suspend layout, suspend binding, virtual mode, etc, etc. I am just not getting this right..

    This where I am:

    Code:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim rows = Me.TblTaskContextDataGridView.Rows.
                                             Cast(Of DataGridViewRow)().
                                             Where(Function(dgvr) CBool(dgvr.Cells("Column1").Value)).
                                             Select(Function(dgvr) DirectCast(dgvr.DataBoundItem, DataRowView).Row)
    
    
            For Each row As DataGridViewRow In Me.TblTaskContextDataGridView.Rows
                Dim checked As Boolean = CType(row.Cells("Column1").Value, Boolean)
                If checked Then
                    row.Cells(2).Value = "7"
    
                End If
            Next
    End Sub
    Don't miss the whole point of the dance...

    https://www.youtube.com/watch?v=qHnIJeE3LAI

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