Stack Exchange
Index was out of range while delete
Why do i get this error(Index was out of range. Must be non-negative and less than the size of the collection.)?
Code :
            object value = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex ].Value;
            object minus = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value;
            object delte = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex + 2].Value;

            if (value is DBNull || minus is DBNull || delte is DBNull) { return; }
            else if (value.Equals("+"))
            {
                produseTableAdapter.PlusCantitate(DateTime.Now, dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex -5].Value.ToString());
                FillData();
                dataGridView1.Rows[e.RowIndex].Selected = true;
            }
            else if (minus.Equals("-"))
            {
                produseTableAdapter.MinusCantitate(DateTime.Now, dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex - 5].Value.ToString());
                FillData();
                dataGridView1.Rows[e.RowIndex].Selected = true;
            }
            else if (delte.Equals("Delete"))
            {
                if (MessageBox.Show("Really ?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
                {
                    produseTableAdapter.DeleteQuery(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex - 5].Value.ToString());
                    FillData();
                    dataGridView1.Rows[e.RowIndex].Selected = true;
                }
                else { return; }
            }

The problem is with the Cells[e.ColumnIndex + 1] && Cells[e.ColumnIndex + 2]. 
Next cell from object value is another button and next after this is another button. How can i get the indexes of this buttons ?

What Event is this code in paste in the Method Event please..

Which line causes the exception?

I imagine the issue is e.ColumnIndex - 5 - why are you doing that?

Don't make us guess where the exception occurs. Spend some time asking a clear question with the necessary information.

i get those errors at : object minus = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value;
object delte = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex + 2].Value;

One of your Rows[] or Cells[] references must be beyond the range of your actual data.  Make sure your row and column indexing, especially in cases where you subtract or add constants, are within the bounds of your data.

I have 3 buttons in every row. How would you suggest to get the index of the second button based by the first button ? Thanks!

Have you verified that object given by the ColumnIndex reference is what you expect it to be?  Adding 1 or 2 to the index should not be a problem as long as the resulting column index is less than the number of columns in the grid.

Maybe it's related to Delete button, when you delete the last row and set the Selected Row to that previously deleted row.

nice Jani, come back to SO :)

Becuase you are trying to access a row which does not exist.
    produseTableAdapter.PlusCantitate(DateTime.Now, dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex -5].Value.ToString());
    FillData();
    dataGridView1.Rows[e.RowIndex].Selected = true;

For example, if you have 4 rows in your grid, in the first line, you are deleting the record from the database and second line, you are reloading the grid data. so now it doesnt have the 4'th row. But you still try to to some operation the 4'th row. in your 3rd line.
Change your 3rd line to this
  if(e.RowIndex>0)
  {
     int newIndex=e.RowIndex-1;
     dataGridView1.Rows[newIndex].Selected = true;
  }

