Home - Forums-.NET - FlyGrid.Net (Windows Forms) - delete rows in selection

FlyGrid.Net (Windows Forms)

.NET Datagrid - Fast, highly customizable, industry standards .NET data grid control for WinForms

This forum related to following products: FlyGrid.Net

delete rows in selection
Link Posted: 11-Nov-2005 00:38
Hello,

i don't know how to delete all selected rows in the grid? I have done the following for one row but it does not seem to work when i do it for multiple rows. can you help me?

Public Sub DeleteRowFromGrid(ByVal dg As FlyGrid, ByVal obj As Object)
            Dim currentRow As Integer
            dg.BeginInit()

            Dim node As NodeBase = dg.Selected
            If Not node Is Nothing Then

               currentRow = node.Index
               Dim dr As DataRowView = CType(node.Value, DataRowView)
               If Not dr Is Nothing Then dr.Delete()
                        If TypeOf (obj) Is DataSet Then
                            DirectCast(obj, DataSet).AcceptChanges()
                        ElseIf TypeOf (obj) Is DataTable Then
                            DirectCast(obj, DataTable).AcceptChanges()
                        Else
                            Exit Sub
                        End If
                  End If
              dg.EndInit()

               Dim newFocused As Integer
              If currentRow  -1 And currentRow < dg.ActivePort.RowCount Then
                    newFocused = currentRow
                Else
                    newFocused = dg.ActivePort.RowCount - 1
                End If

                If dg.Rows.Items.Count > 0 Then dg.Selected = dg.ActivePort.Rows.Items(newFocused)
            End Try
        End Sub
Link Posted: 11-Nov-2005 04:27
This code example shows how to dlete selected rows from databound FlyGrid
[VB.Net]
  Private Function DeleteSelection(ByVal flyGrid As FlyGrid) As Boolean
    Dim result As Boolean = False
    flyGrid.BeginInit()
    Try
      For Each node As NodeBase In flyGrid.Rows.GetSelection()
        Dim row As DataRowView = CType(node.Value, DataRowView)
        If Not (row Is Nothing) Then
          row.Delete()
        End If
      Next
      result = True
    Finally
      flyGrid.EndInit()
    End Try
    Return result
  End Function

  Private Sub deleteSelectedBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles deleteSelectedBtn.Click
    If (DeleteSelection(Me.flyGrid1)) Then
      'accept changes
      Dim dt As DataTable = CType(Me.flyGrid1.Rows.DataSource, System.Data.DataTable)
      If Not (dt Is Nothing) Then
        dt.AcceptChanges()
      End If
    End If
  End Sub
Link Posted: 14-Nov-2005 23:45
This code is not correct. After row.Delete() the index of the nodes change, therefore the next node to be deleted is not synchronized anymore with the grid indexes :


row0
row1          <- selected for deletion
row2          <- selected for deletion
row3

after row1 has been deleted, the next node to be deleted should have index 2 but the node with index 2 is now row3 instead of row2.

I've tried the new sample that shows how to do multiple selection delete, and it actually has the same problem as the one I'm describing, so sample does not work as expected.

Can you help?

Thanks
Link Posted: 16-Nov-2005 08:56
After deleting all nodes is refreshed to reflect result of multiple deletions.
Part of your code will works for selection fo new focused node:
[VB]
'getting current row index
Dim currentRow = dg.Selected.Index;
' rows deletion procedure
'
' .....
'
' after deletion
Dim newFocused As Integer
If currentRow  -1 And currentRow < dg.ActivePort.RowCount Then
    newFocused = currentRow
Else
    newFocused = dg.ActivePort.RowCount - 1
End If
  
If dg.Rows.Items.Count > 0 Then
    dg.Selected = dg.ActivePort.Rows.Items(newFocused)            
Link Posted: 18-Nov-2005 04:55
Still... i don' t understand your sample on multiple delete. To me it seem it does not work correctely (add 4 rows and delete row 2 and 3 for example...)
Link Posted: 19-Nov-2005 04:38
Reason of problem - dg.ActivePort.RowCount return actual row count (with AddNew row), here is correct code:
[VB]
  Private Sub deleteSelectedBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles deleteSelectedBtn.Click
    'getting current row index
    Dim currentRow As Integer = Me.dgParams.Selected.Index
    If (DeleteSelection(Me.dgParams)) Then
      'accept changes if deletion is succesfully finished
      Dim dt As DataTable = CType(Me.dgParams.Rows.DataSource, System.Data.DataTable)
      If Not (dt Is Nothing) Then
        dt.AcceptChanges()
      End If
      ' after deletion
      ' also you can get row count from data table
      Dim rowCount As Integer = Me.dgParams.ActivePort.RowCount
      If ((Me.dgParams.ActivePort.Rows.Options And RowsOptions.ShowAddNewRow)  0) Then
        rowCount = rowCount - 1
      End If

      Dim newFocused As Integer
      If currentRow  -1 And currentRow < rowCount Then
        newFocused = currentRow
      Else
        newFocused = rowCount - 1
      End If

      If Me.dgParams.Rows.Items.Count > 0 Then
        Me.dgParams.Selected = Me.dgParams.ActivePort.Rows.Items(newFocused)
      End If
    End If
  End Sub
Link Posted: 20-Nov-2005 22:20
Well have you actually tried your sample in the way I've described earlier? Because even with new code, it still doesn't work as expected.
Link Posted: 22-Nov-2005 07:44
Yes, we've tried this sample and update this sample in the list of VB.Net samples, where you can find it and download full VB project (you can download this sample here).
Link Posted: 22-Nov-2005 22:28
I'm sorry but this sample still doesn't work.
I get an exception in deleteSelectedBtn_Click because dgParams.Selected is nothing most of the time when I do multiple selection.
Also I get an exception in
Me.dgParams.Selected = Me.dgParams.ActivePort.Rows.Items(newFocused)

as newFocused happens to be -1 sometimes.

Can you get it to work please? That'd be very helpful.

Thank you
Link Posted: 23-Nov-2005 11:24
Please download this sample once again, we've made some code improvements to delete single selections and avoid deletions in unselected state or AddNewrow node.