Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过单击索引超出范围_C#_Winforms_Datagridview - Fatal编程技术网

C# 通过单击索引超出范围

C# 通过单击索引超出范围,c#,winforms,datagridview,C#,Winforms,Datagridview,我需要使用cellClick\u事件获取上次编辑的datagridview单元格的行索引 我试着这样做: private void DataGridView_CellClick(object sender, DataGridViewCellEventArgs e) { var lastIndex = DataGridView.SelectedRows[DataGridView.SelectedRows.Count - 1].Index; var c

我需要使用
cellClick\u事件获取上次编辑的
datagridview
单元格的行索引

我试着这样做:

    private void DataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        var lastIndex = DataGridView.SelectedRows[DataGridView.SelectedRows.Count - 1].Index;
        var currentIndex = DataGridView.CurrentRow.Index;
        if (currentIndex != lastIndex)
        {
            //code
        }
        else
        {
        }
    }
但它不起作用。我得到一个错误:

index out of bound
  • 如何使用
    cellClick\u事件获取datagridview最后编辑的
    单元格的
    行索引
  • 是否存在除datagridview.cellclick之外的任何其他事件来解决它

将datagridview编辑模式设置为EditoNet。然后,当您在单元格中单击时,它将为您提供上次修改的行的索引,这是您已插入的单元格的行索引

int i = dataGridView.CurrentRow.Index;

或者,如果在编辑上一行的索引之后,您可以将上述值记录在“Cell_Leave”事件句柄中。

假设您要将单击的行与当前编辑的行进行比较,这应该可以实现这一目的

void DataGridView_CellClick(object sender, DataGridViewCellEventArgs e) {

    int currentIndex = DataGridView.CurrentRow.Index;
    int clickedCellRowIndex = e.RowIndex;

    if (currentIndex == clickedCellRowIndex) {
        //do work
    }

无法使用CellEndEdit事件。调用此选项后,您将可以访问刚刚编辑的行。但是,您必须找到一种方法来查看它是否确实被编辑过,因为我认为即使编辑被取消,它也会被调用

这可能对你有所帮助

   private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[e.ColumnIndex];
    }

您想要上次编辑的行吗?与上次选中的行相同。否。我需要当前修改的行。在datagridview事件中找不到任何内容。我认为索引不会相同,您应该检查
CurrentRow
是否已经是集合的一部分,或者不是类似
.SelectedRows.Contains(datagridview.CurrentRow)
(但这是用户选择的行,不必编辑)的内容。我就是这么做的。。它给我当前的行索引。我需要上次编辑的行的索引。如果您希望它在编辑单元格时为您提供行索引,请参阅我的更新。