linguini
linguini

Reputation: 1939

Argument Out of range exception Error

until yesterday the below code was workig fine but today I'm getting a error message Argument out of exception & index out of range.

Wha t i'm tryring to do here, from the index 3(4th col) of the last row, take that cell value & put in to the col 3 (index 2) cell. When i type it in the last cell (from bottom to top), i'm getting the above error message.

Please help me.

private void datagridview_CellValidated(object sender, CellValidatedEventArgs e)
{

if (e.ColumnIndex != 3)
    return;
int nextRowIndex = e.RowIndex -1;
int lastRowIndex = datagridview.Rows.Count;
try
{

        if (nextRowIndex <= lastRowIndex)
        {
            var valuesForcell = datagridview.Rows[e.RowIndex].Cells[3].Value.ToString();
            datagridview.Rows[nextRowIndex].Cells[2].Value = valuesForcell;
            datagridview.Rows[nextRowIndex].Cells[2].ReadOnly = true;
            datagridview.Rows[nextRowIndex].Cells[2].Style.ForeColor = Color.MediumVioletRed;
            datagridview.ClearSelection();
            datagridview.SelectionMode = GridViewSelectionMode.CellSelect;
            datagridview.Rows[nextRowIndex].Cells[3].BeginEdit();

        }

}
catch (Exception exception) { }


}

enter image description here enter image description here

Upvotes: 0

Views: 3573

Answers (5)

Mr_Green
Mr_Green

Reputation: 41832

change your if statement to something like this:

 if ((nextRowIndex < lastRowIndex) && (nextRowIndex >= -1))

-1 is valid rowIndex if you want to include even the Rowheader.

If you dont want to include RowHeader then replace -1 with 0.

Hope this helps.

Upvotes: 1

Tobia Zambon
Tobia Zambon

Reputation: 7619

if I understand well you should have:

private void datagridview_CellValidated(object sender, CellValidatedEventArgs e)
{

if (e.ColumnIndex != 3)
    return;
int nextRowIndex = e.RowIndex -1;
try
{

        if (nextRowIndex >=0 )
        {
            var valuesForcell = datagridview.Rows[e.RowIndex].Cells[3].Value.ToString();
            datagridview.Rows[nextRowIndex].Cells[2].Value = valuesForcell;
            datagridview.Rows[nextRowIndex].Cells[2].ReadOnly = true;
            datagridview.Rows[nextRowIndex].Cells[2].Style.ForeColor = Color.MediumVioletRed;
            datagridview.ClearSelection();
            datagridview.SelectionMode = GridViewSelectionMode.CellSelect;
            datagridview.Rows[nextRowIndex].Cells[3].BeginEdit();

        }

}
catch (Exception exception) { }


} 

You are moving values bottom-up right?

Upvotes: 2

Nikola Davidovic
Nikola Davidovic

Reputation: 8656

When your e.RowIndex is 0, nextRowIndex is -1 and you are trying to access

datagridview.Rows[-1].Cells[2].Value = valuesForcell;

which throws the Exception

Upvotes: 0

Honza Brestan
Honza Brestan

Reputation: 10947

It looks like you are validating a cell in the 0th row, so the nextRowIndex evaluates as -1, which is obviously not a valid array index.

Upvotes: 1

Habib
Habib

Reputation: 223207

If e.RowIndex returns you 0 first row, then your nextRowIndex will be set to -1 which is wrong.

also your lastRowIndex should be one less than the Row count.

int lastRowIndex = datagridview.Rows.Count - 1;

I see that you are not referencing the row on lastRowIndex, you may modify your check to:

if (nextRowIndex < lastRowIndex)

Upvotes: 1

Related Questions