0

Sir i got index out of range exception in these code Can anybody explain me where i did mistake

if (SelectedRowtt > -1)
{   
    dgvSItt.Rows[SelectedRowtt].Cells["Item_Code"].Value =txtItemCode.Text;
    dgvSItt.Rows[SelectedRowtt].Cells["Description"].Value = txtDescription.Text;
    dgvSItt.Rows[SelectedRowtt].Cells["Qty"].Value = Convert.ToDecimal(txtQty.Text).ToString(Program.gDigitsAfterDecimal);
    dgvSItt.Rows[SelectedRowtt].Cells["UOM"].Value = Convert.ToDecimal(txtUOM.Text).ToString(Program.gDigitsAfterDecimal);

    gvSItt.Rows[SelectedRowtt].Cells["Amount"].Value = Convert.ToDecimal(txtAmount.Text).ToString(Program.gDigitsAfterDecimal);
    dgvSItt.Rows[SelectedRowtt].Cells["Discount"].Value = Convert.ToDecimal(txtRate.Text).ToString(Program.gDigitsAfterDecimal);
    SelectedRowtt = -1;
}
else
{
    IsDO = false;
    dgvSItt.Rows.Add();
    SelectedRowtt = -1;
}
Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74

2 Answers2

0

You get that message when you have used an index on an array (or collection) that is not valid. For instance you may have an array defined for myArray[0] up to myArray[10]. If you tried to access myArray[-1] or myArray[11] you would get the error.

In your case it could be on the rows or the cells collections.

Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74
Astra Bear
  • 2,646
  • 1
  • 20
  • 27
0

Depends on how you get SelectedRowtt. Usualy such exception happens because indexes start with 0 and end with Rows.Count -1 and you try to get dgvSItt.Rows[Rows.Count].

Robert
  • 5,278
  • 43
  • 65
  • 115
f1sherox
  • 349
  • 2
  • 16