简体   繁体   中英

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..!

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).

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)

    int lastRowIndex = SecondaryGridView.Rows.Count - 1;

This should correct your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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