DataGridView 导航到下一行

5

我有一个C# WinForms应用程序,现在我想让一个按钮能够选择当前选定行后的下一行。

到目前为止,我的代码是:

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;
  }

但运行时会抛出异常。请问有谁能指出我可能出了什么问题。抛出的错误是:索引超出范围
8个回答

9

试试这个:

 int nRow;
private void Form1_Load(object sender, EventArgs e)
{

    nRow = dataGridView1.CurrentCell.RowIndex;
}

private void button1_Click(object sender, EventArgs e)
{
    if (nRow < dataGridView1.RowCount )
    {
        dataGridView1.Rows[nRow].Selected = false;
        dataGridView1.Rows[++nRow].Selected = true;
    }
}

谢谢。这个功能在某种程度上是有效的。但我希望它能取消选择之前的行。 - Daniel Flannery
这很完美,谢谢。现在我明白了我应该做什么。 - Daniel Flannery

4

首先,将 datagridview 的 "Multiselect" 属性设置为 false。

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

它将选择数据网格视图中的下一行。

3

选择行和单元格是更好的解决方案。这个解决方案将行指示器移动到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;
        }
    }

1

它在这里:

dataGridView1.SelectedRows[selectedRowCount]

如果您选择了3行,则selectedRowCount = 3,并且有三个索引为0、1、2的行。
您正在尝试访问不存在的#3。

啊,我明白了。你能提供一种实现我想要的结果的方法吗? - Daniel Flannery

0
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;
            }
        }
       

    }

你的答案可以通过添加更多支持信息来改进。请编辑以添加进一步详细信息,例如引用或文档,以便他人可以确认你的答案是否正确。您可以在帮助中心找到有关如何撰写良好答案的更多信息。 - Community

0

这个示例用于读取DataGridView中第4列的单元格或列的值

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

0
我更喜欢这种行选择方式:
首先检查是否没有多选:数据数量 然后获取所选单元格(或行):行索引
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 

    }

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接