none
Remove datagridview row on checkbox check RRS feed

  • Question

  • here's my code to add a row from one dgvw to another if a row's checkbox column is checked :

        Private Sub userdatagrid_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles userdatagrid.CurrentCellDirtyStateChanged
            If userdatagrid.IsCurrentCellDirty Then
                userdatagrid.CommitEdit(DataGridViewDataErrorContexts.Commit)
                Dim c, t As Integer
                Selected.dg2.Columns.Clear()
                For t = 0 To userdatagrid.Columns.Count - 1
                    Selected.dg2.Columns.Add(userdatagrid.Columns(t).Clone())
                Next
                For c = 0 To userdatagrid.Rows.Count - 1
                    If userdatagrid.Rows(c).Cells(0).Value = True Then
                        Selected.dg2.Rows.Add(userdatagrid.Rows(c).Cells(0).Value, userdatagrid.Rows(c).Cells(1).Value, userdatagrid.Rows(c).Cells(2).Value, userdatagrid.Rows(c).Cells(3).Value, userdatagrid.Rows(c).Cells(4).Value, userdatagrid.Rows(c).Cells(5).Value, userdatagrid.Rows(c).Cells(6).Value, userdatagrid.Rows(c).Cells(7).Value, userdatagrid.Rows(c).Cells(8).Value, userdatagrid.Rows(c).Cells(9).Value, userdatagrid.Rows(c).Cells(10).Value, userdatagrid.Rows(c).Cells(11).Value, userdatagrid.Rows(c).Cells(12).Value, userdatagrid.Rows(c).Cells(13).Value, userdatagrid.Rows(c).Cells(14).Value, userdatagrid.Rows(c).Cells(15).Value, userdatagrid.Rows(c).Cells(16).Value, userdatagrid.Rows(c).Cells(17).Value, userdatagrid.Rows(c).Cells(18).Value, userdatagrid.Rows(c).Cells(19).Value, userdatagrid.Rows(c).Cells(20).Value, userdatagrid.Rows(c).Cells(21).Value, userdatagrid.Rows(c).Cells(22).Value, userdatagrid.Rows(c).Cells(23).Value, userdatagrid.Rows(c).Cells(24).Value, userdatagrid.Rows(c).Cells(25).Value, userdatagrid.Rows(c).Cells(26).Value, userdatagrid.Rows(c).Cells(27).Value, userdatagrid.Rows(c).Cells(28).Value, userdatagrid.Rows(c).Cells(29).Value, userdatagrid.Rows(c).Cells(30).Value)
                    Else
                    End If
                    Selected.dg2.Columns(0).Visible = False
                Next
            Else
            End If

    My question is how do i remove the row if the checkbox is unchecked after being checked first ?

    i tried : 

     Selected.dg2.Rows.Remove(userdatagrid.Rows(c).Cells(0).Value, userdatagrid.Rows(c).Cells(1).Value, userdatagrid.Rows(c).Cells(2).Value, userdatagrid.Rows(c).Cells(3).Value, userdatagrid.Rows(c).Cells(4).Value, userdatagrid.Rows(c).Cells(5).Value, userdatagrid.Rows(c).Cells(6).Value, userdatagrid.Rows(c).Cells(7).Value, userdatagrid.Rows(c).Cells(8).Value, userdatagrid.Rows(c).Cells(9).Value, userdatagrid.Rows(c).Cells(10).Value, userdatagrid.Rows(c).Cells(11).Value, userdatagrid.Rows(c).Cells(12).Value, userdatagrid.Rows(c).Cells(13).Value, userdatagrid.Rows(c).Cells(14).Value, userdatagrid.Rows(c).Cells(15).Value, userdatagrid.Rows(c).Cells(16).Value, userdatagrid.Rows(c).Cells(17).Value, userdatagrid.Rows(c).Cells(18).Value, userdatagrid.Rows(c).Cells(19).Value, userdatagrid.Rows(c).Cells(20).Value, userdatagrid.Rows(c).Cells(21).Value, userdatagrid.Rows(c).Cells(22).Value, userdatagrid.Rows(c).Cells(23).Value, userdatagrid.Rows(c).Cells(24).Value, userdatagrid.Rows(c).Cells(25).Value, userdatagrid.Rows(c).Cells(26).Value, userdatagrid.Rows(c).Cells(27).Value, userdatagrid.Rows(c).Cells(28).Value, userdatagrid.Rows(c).Cells(29).Value, userdatagrid.Rows(c).Cells(30).Value)
    

    But it gives me a public override error!Any solution ?

    Tuesday, October 31, 2017 5:13 PM

All replies

  • Take a look at the following which may assist.

    https://code.msdn.microsoft.com/Bascis-to-advance-working-7b757af3/sourcecode?fileId=146990&pathId=1190653684


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, October 31, 2017 5:37 PM
    Moderator
  • Take a look at the following which may assist.

    https://code.msdn.microsoft.com/Bascis-to-advance-working-7b757af3/sourcecode?fileId=146990&pathId=1190653684


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    really don't get anything relevant to my issue...any help 

    ?

    Tuesday, October 31, 2017 6:13 PM
  • Take a look at the following which may assist.

    https://code.msdn.microsoft.com/Bascis-to-advance-working-7b757af3/sourcecode?fileId=146990&pathId=1190653684


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    really don't get anything relevant to my issue...any help 

    ?


    Okay guess you don't get it.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, October 31, 2017 6:19 PM
    Moderator
  • Hi Aousaf Rashid,

    In advance, sorry this is not an answer, but a request.

    You have many posts about DataGridView and other topics.
    And good-hearted people have provides would-be answers, but you have never marked as answer.
    Why don't you mark and close your issue?  
    Regards,

    Ashidacchi

    Wednesday, November 1, 2017 12:31 AM
  • Hi Aousaf Rashid,

    You encountered public override error at Selected.dg2.Rows.Remove. I don't know if DataGridView.Rows.Remove can take so many parameters that you pass in. Even it exists(maybe from override), I don't think it is a good way to remove rows.

    When you use Selected.dg2.Rows.Add, you must have Columns first, then the executor knows that the first parameter you pass in Selected.dg2.Rows.Add should be filled in the first column, second filled in the second, and so on.

    But when you use Selected.dg2.Rows.Remove and pass several parameters in, how does the executor knows which row should be removed? Row whose first column value equals first parameter, second column value equals second parameter, and so on? I think this can make sense but too complex.

    I recommend you use the following built-in methods of DataGridView to remove rows.

    Public Overridable Sub RemoveAt(index As Integer)
    Public Overridable Sub Remove(dataGridViewRow As DataGridViewRow)

    Above is my suggestion about how to remove rows.

    What's more, I think it is also not a good way to clear all rows and add them again all the time. When you want to add a row, just check whether the row is already in, if not, add it. When you want to remove a row, just get the index of it and remove it. Below is my codes to add and remove rows according to the status of checkbox.

    Imports System.ComponentModel
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            dgv.Rows.Add(False, 1, "Tony", 40)
            dgv.Rows.Add(False, 2, "Steve", 70)
            dgv.Rows.Add(False, 3, "Peter", 16)
        End Sub
        Private Sub dgv_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles dgv.CurrentCellDirtyStateChanged
    
            If dgv.IsCurrentCellDirty And dgv.CurrentCell.ColumnIndex = 0 Then 'If current cell is not checkbox, skip
                dgv.CommitEdit(DataGridViewDataErrorContexts.Commit)
    
                If dgv.CurrentCell.Value Then 'If checkbox checked and current id doesn't exist in dgvcopy, then add
                    For Each row As DataGridViewRow In dgvcopy.Rows
                        If row.Cells(0).Value = dgv.CurrentRow.Cells(1).Value
                            Return
                        End If
                    Next 
                    dgvcopy.Rows.Add(dgv.CurrentRow.Cells(1).Value, dgv.CurrentRow.Cells(2).Value, dgv.CurrentRow.Cells(3).Value)
    
                Else 'If checkbox unchecked, then find row in dgvcopy you want to remove
                    Dim index As Integer = -1
                    For i As Integer = 0 To dgvcopy.Rows.Count - 1
                        If dgvcopy.Rows(i).Cells(0).Value = dgv.CurrentRow.Cells(1).Value
                            index = i
                            Exit For
                        End If
                    Next
                    If index >= 0 Then
                        dgvcopy.Rows.RemoveAt(index)
                    End If
                End If
                dgvcopy.Sort(dgvcopy.Columns(0), ListSortDirection.Ascending)
            End If
        End Sub
    End Class

    Btw, thanks Ashidacchi for good suggestion to benefit community and its members. You can take it into consideration and kindly mark reply that helps you as answer, this will benefit other community members with same or similar questions, at the same time, this shows your recognition and will make community members willing to be more helpful.

    Thanks a lot,

    Charles He


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.





    Wednesday, November 8, 2017 6:23 AM