简体   繁体   English

从最后一行更新datagridview

[英]Update datagridview from last row

I'm trying to update cell vales from last row.I got 4 cols and 4 rows, I want to take the 4th col 4th cell value to 3rd row 3rd cell, like that Take the 4th col 3rd cell value to 2nd row 3rd cell..etc..! 我正在尝试从最后一行更新单元格值。我有4个列和4行,我想将第4个列第4个单元格的值带到第3行第3个单元格,就像那样将第4个列第3个单元格的值带到第2行第3个单元格..等等..!

What is the logical mistake here, how can ifix this?? 这里的逻辑错误是什么?

The below code is working but at the last row(from botton to top) i'm getting an indexing error(Argument out of range). 下面的代码正在工作,但是在最后一行(从botton到顶部),我遇到了索引错误(参数超出范围)。

if (e.ColumnIndex != 3)
    return;
int nextRowIndex = e.RowIndex - 1;
int lastRowIndex = SecondaryGridView.Rows.Count + 1;
if (nextRowIndex <= lastRowIndex)
{
    var value = SecondaryGridView.Rows[e.RowIndex].Cells[3].Value.ToString();
    SecondaryGridView.Rows[nextRowIndex].Cells[2].Value = value;
}

A simple try catch fixed the problem. 一个简单的尝试捕获解决了这个问题。 Buy I don't know if this is the correct solution. 购买我不知道这是否是正确的解决方案。

Please correct me, if I'm doing anything wrong, here. 如果我做错了什么,请在这里纠正我。

if (e.ColumnIndex != 3)
    return;

int nextRowIndex = e.RowIndex - 1;
int lastRowIndex = SecondaryGridView.Rows.Count;

try
{
    if (nextRowIndex <= lastRowIndex)
    {
        var value = SecondaryGridView.Rows[e.RowIndex].Cells[3].Value.ToString();
        SecondaryGridView.Rows[nextRowIndex].Cells[2].Value = value;
    }
}
catch(Exception exception){}

To get last row index (Zero based index -- RowCount - 1) 获取最后一行索引(基于零的索引-RowCount-1)

    int lastRowIndex = SecondaryGridView.Rows.Count - 1;

This should correct your problem. 这应该可以解决您的问题。

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

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