Reposition and select row after deleted row in RadGridView Telerik WinForm

1 Answer 36 Views
GridView
fabrizio
Top achievements
Rank 1
Veteran
fabrizio asked on 24 Sep 2024, 09:56 AM

Good morning
in a RadGridView in the event of a button I delete a row from the database and I would like to reposition and select the row after the deleted one. The problem is that I can't figure out how to get the id of the row after the deleted one. To complicate things I add that the rows are filtered by the filtering row of the RadGridView. I attach code to make it clearer.

I think the problem is that to delete the row I take the index from the first cell of the row. But I don't understand how to take the index of the row of the radgridview

Fabrizio


    Private Sub btnEliminaLotto_Click(sender As Object, e As EventArgs) Handles btnEliminaLotto.Click

        'Trova id selezionato con ChildRows
        propIdLottoMateriale = gridLottoMateriale.ChildRows(gridLottoMateriale.CurrentRow.Index).Cells(0).Value

        Dim result As DialogResult

        If Not (propIdLottoMateriale = Nothing) Then
            If gridLottoMateriale.SelectedRows.Count > 0 Then
                result = MessageBox.Show(" Sei sicuro di voler eliminare il Test Report N° : " & propIdLottoMateriale & " ? ", "Elimina test Report in Database", MessageBoxButtons.OKCancel)
                If result = DialogResult.Cancel Then
                    Exit Sub
                End If

                If result = Windows.Forms.DialogResult.OK Then
                    EliminaLottoMateriale(propIdLottoMateriale)

                    'Controllo se il filtro esiste ancora
                    If (gridLottoMateriale.IsInEditMode) Then
                        gridLottoMateriale.EndEdit()
                    End If

                    'Toglie il filtro
                    gridLottoMateriale.FilterDescriptors.Clear()

                    'PopolaGridLotto()
                    'Aggiorna il dataset binding con la grid
                    TblNumeroLottoMaterialeTableAdapter.Fill(DsNumeroLottoMateriale1.tblNumeroLottoMateriale)

                    'Riposiziona la griglia sulla riga dopo quella eliminata
                    gridLottoMateriale.CurrentRow = gridLottoMateriale.ChildRows(indexRigaPerAggiorna + 1)
                    gridLottoMateriale.CurrentRow.IsSelected = True

                Else
                        Exit Sub
                End If
            Else
                MessageBox.Show("Seleziona una riga !!")
            End If

        Else

        End If

    End Sub



    Public Function EliminaLottoMateriale(ByVal id As Integer)
        Dim deleteCommand As DbCommand = Nothing
        Dim rowsAffected As Integer
        Try
            deleteCommand = _db.GetStoredProcCommand("spDeleteQueryLotto")
            _db.AddInParameter(deleteCommand, "id", DbType.Int32, id)
            rowsAffected = _db.ExecuteNonQuery(deleteCommand)
        Catch ex As Exception
            MessageBox.Show("Errore EliminaLottoMateriale : " & ex.Message)
        End Try
        Return rowsAffected
    End Function

1 Answer, 1 is accepted

Sort by
0
Nadya | Tech Support Engineer
Telerik team
answered on 26 Sep 2024, 10:28 AM

Hello, Fabrizio,

By default, when you delete a row from RadGridView, the grid automatically selects the next row from the collection. There is no need to do it manually. If I understand you correctly you are trying to get the index of the row after the deleted row. 

When you delete a record from the DataSource directly, the RadGridView.RowsChanged event is fired. You can use the GridViewCollectionChangedEventArgs.OldStartingIndex in order to get the row index of deleted row. Then, the next row index will be OldStartingIndex+1. Please refer to the following code snippet:

private void RadGridView1_RowsChanged(object sender, GridViewCollectionChangedEventArgs e)
{
    int deletedRowindex = e.OldStartingIndex;
    int nextRowIndex = deletedRowindex + 1;
}

I hope this information helps. If you have other questions, please let me know. 

Regards,
Nadya | Tech Support Engineer
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
GridView
Asked by
fabrizio
Top achievements
Rank 1
Veteran
Answers by
Nadya | Tech Support Engineer
Telerik team
Share this question
or