winforms DataGridView导航到下一行

xghobddn  于 2022-11-16  发布在  其他
关注(0)|答案(8)|浏览(207)

我有一个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;
  }

但是在运行这个程序时,它抛出了一个异常。有人能指出我哪里出错了吗?抛出的错误是:Index is out of range

vawmfj5a

vawmfj5a1#

试试这个:

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

szqfcxe22#

首先,将datagridview的“多重选择”属性设置为false。

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

它将选择DataGridView中的下一行。

dauxcl2d

dauxcl2d3#

选择'行和单元格'可获得更好解决方案此解决方案在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;
        }
    }
vc9ivgsu

vc9ivgsu4#

它在这里:

dataGridView1.SelectedRows[selectedRowCount]

如果您选取了3个数据列,则selectedRowCount = 3,而且有3个数据列具有索引:0、1、2。
您访问的页面不存在!

vohkndzv

vohkndzv5#

此示例读取的值单元格或列是datagridview的编号4

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

qcbq4gxm6#

我更喜欢这样的行选择:
首先检查是否没有多选:number_of_data然后获取所选单元格(或行):行索引

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 

    }
x6h2sr28

x6h2sr287#

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

    }
flvtvl50

flvtvl508#

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

相关问题