'DataGridView navigating to next row

I have a C# winforms application and I am trying to get a button working that will select the next row in a datagridview after the one curently selected.

The code I have so far is:

private void button4_Click(object sender, EventArgs e)
{
  try
  {
    Int32 selectedRowCount = dataGridView1.Rows.GetRowCount(DataGridViewElementStates.Selected);

    // index out of range on this line
    dataGridView1.Rows[dataGridView1.SelectedRows[selectedRowCount].Index].Selected = true;

    dataGridView1.FirstDisplayedScrollingRowIndex = selectedRowCount + 1;
  }
  catch (Exception ex)
  {
    return;
  }

But on running this it throws an exception. Could anyone point out where I may be going wrong. The thrown error is: Index is out of range



Solution 1:[1]

First, set "Multiselect" property of datagridview to false.

int currentRow = dataGridView1.SelectedRows[0].Index;
if (currentRow < dataGridView1.RowCount)
{
    dataGridView1.Rows[++currentRow].Selected = true;
}

It will select the next row in the datagridview.

Solution 2:[2]

It's here:

dataGridView1.SelectedRows[selectedRowCount]

If you have 3 selected rows then selectedRowCount = 3 and there are three rows with indexes: 0, 1, 2.

You are trying to access #3 which doesn't exist.

Solution 3:[3]

Select Row and Cell for better solution. This solution move row indicator on DataGridView.

    private void _GotoNext(object sender, EventArgs e)
    {
        int currentRow = DataGridView1.SelectedRows[0].Index;
        if (currentRow < DataGridView1.RowCount - 1)
        {
            DataGridView1.Rows[++currentRow].Cells[0].Selected = true;
        }
    }

    private void _GotoPrev(object sender, EventArgs e)
    {
        int currentRow = DataGridView1.SelectedRows[0].Index;
        if (currentRow > 0)
        {
            DataGridView1.Rows[--currentRow].Cells[0].Selected = true;
        }
    }

Solution 4:[4]

this example to read value cell or column is number 4 of datagridview

        int courow = dataGridView1.RowCount-1;
        for (int i=0; i < courow; i++)
        {
            MessageBox.Show(dataGridView1.Rows[i].Cells[4].Value.ToString());
        }

Solution 5:[5]

I prefer this row selection :

First check if no multiselect : number_of_data Then get the select cell (or row) : row_index

private void next_click(object sender, EventArgs e)
    {
        int number_of_data = dataGridView.SelectedRows.Count;
        if (number_of_data > 1) return;

        int row_index = dataGridView.SelectedCells[0].RowIndex;

        if (row_index < dataGridView.RowCount-1)
        {
            dataGridView.Rows[row_index++].Selected = false;
            dataGridView.Rows[row_index].Selected = true;
        }

        // Do something 

    }

Solution 6:[6]

enter code here  private void Form1_Load(object sender, EventArgs e)
    {
            X = dataGridView1.CurrentCell.RowIndex;//int x;
    }

enter code here  private void button2_Click(object sender, EventArgs e)
    {
        if (dataGridView1.Rows.Count > 0)
        {
            this.dataGridView1.ClearSelection();
            dataGridView1.Rows[0].Selected = true;
        }
    }
enter code here  private void button3_Click(object sender, EventArgs e)
    {
       

        if (X < dataGridView1.RowCount)
        {
            if (X != dataGridView1.RowCount - 1)
            {
                dataGridView1.ClearSelection();
                dataGridView1.Rows[++X].Selected = true;

            }
            else
            {
                button2_Click(sender, e);//this else with make it loop 
                X = 0;
            }
        }
       

    }

Solution 7:[7]

dgv_PhotoList.Rows[dgv_PhotoList.CurrentRow.Index+1].Selected = true;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Osama Rizwan
Solution 2 Steve Wellens
Solution 3 Pažout
Solution 4 user3771393
Solution 5 Dominique Mathieu
Solution 6 OUSSAMA ELFALLAH
Solution 7 Ali Moazzen Safaie