Check if the selected line is the penultimate of datagridview

Asked

Viewed 137 times

-2

I have a dataGridView on the screen and need that when the penultimate record is selected, I send the selection to the next line of the grid, which in case would be the last.

Summary of the problem:

I need to know when the selection is on the penultimate line of the grid, to apply a code to go to the next line, if this selection is on the penultimate line.

Part of the code below, the part going to the next line works, only the check is missing if it is the penultimate line that is selected.

private void MoveNext()
{
    //Select a proxima linha caso:
    //int last = this.dgvDados.CurrentRow.
    if (//verificação aqui)
    {
        int index = this.dgvDados.CurrentRow.Index + 1;
        index = index % this.dgvDados.Rows.Count;
        this.dgvDados.CurrentCell = this.dgvDados.Rows[index].Cells[this.dgvDados.CurrentCell.ColumnIndex];
    }
}

1 answer

0


dgvDados.CurrentRow.Index returns the current row index.

dgvDados.Rows.Count returns the number of rows in the grid. That is to say, dgvDados.Rows.Count - 1 is the index of the last line, so, dgvDados.Rows.Count - 2 is the penultimate line index.

So:

if(dgvDados.CurrentRow.Index == dgvDados.Rows.Count - 2)
{
    // A linha selecionada é a penúltima
}
  • 1

    It was much simpler than I imagined, thank you very much!

  • 1

    I’m glad I could help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.