0
votes

I want to programmatically delete the last row in my Datagridview when a user clicks a button. As of now, the program will scroll down and highlight the last row, but only deletes the row where the index is at. So, I need to move the indexer (triangle indicator) to the last row. How do I do this??

 If LftMtr_Data_Grid.RowCount >= 2 Then
        LftMtr_Data_Grid.FirstDisplayedScrollingRowIndex = LftMtr_Data_Grid.RowCount - 2 'scroll to the last row
        LftMtr_Data_Grid.Rows(LftMtr_Data_Grid.RowCount - 2).Selected = True 'select the last row
        LftMtr_Data_Grid.Rows.RemoveAt(LftMtr_Data_Grid.CurrentRow.Selected) 'delete selected row
    End If

Thank you

1
add a language tag, this is VB or VB.net i guessTejesh Alimilli

1 Answers

0
votes

The CurrentRow is the row containing the active cell. By setting the Selected property to true, the row is only highlighted but not active.

To make the last row as active i.e. to make the row as CurrentRow use the CurrentCell property. Set any of the cell in the last row as CurrentCell before removing the row.

LftMtr_Data_Grid.CurrentCell = LftMtr_Data_Grid.Rows(LftMtr_Data_Grid.RowCount - 1).Cells(0);
LftMtr_Data_Grid.Rows.RemoveAt(LftMtr_Data_Grid.CurrentRow.Index);