简体   繁体   English

DataGridView 导航到下一行

[英]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.我有一个 C# winforms 应用程序,我试图让一个按钮工作,该按钮将在当前选择的一行之后选择 datagridview 中的下一行。

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抛出的错误是: Index is out of range

try this:尝试这个:

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

First, set " Multiselect " property of datagridview to false.首先,将datagridview的“ Multiselect ”属性设置为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.它将选择 datagridview 中的下一行。

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.如果您有 3 个选定的行,则 selectedRowCount = 3 并且有三个带有索引的行:0、1、2。

You are trying to access #3 which doesn't exist.您正在尝试访问不存在的 #3。

Select Row and Cell for better solution.选择行和单元格以获得更好的解决方案。 This solution move row indicator on DataGridView.此解决方案在 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;
        }
    }

this example to read value cell or column is number 4 of datagridview这个读取值单元格或列的示例是 datagridview 的第 4 个

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

I prefer this row selection :我更喜欢这个行选择:

First check if no multiselect : number_of_data Then get the select cell (or row) : row_index首先检查是否没有多选:number_of_data 然后获取选择单元格(或行):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 

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM