Я имею datagridview
имеет 3 колонкиошибка при попытке редактирования существуют строки в DataGridView
Колонка 1 = PRODUCTID
Колонка 2 = Цена
Колонка 3 = Количество
I я пытаюсь добавить количество на 1 (+1), если идентификатор продукта и с той же ценой повторяется. и должны добавить новую строку в datagridview
, если идентификатор продукта не существует в datagridview
Моя проблема Я получаю сообщение об ошибке в части else if (!ProductIDExist)
что говорит
Индекс был вне диапазона. Должен быть неотрицательным и меньше размера коллекции.
private void SelectedProductData()
{
int ItemCount = DGV_INVOICE.Rows.Count;
bool ProductIDExist = false;
string connstr = @"Data Source=orcl; User Id=user; password=pwd;";
string cmdtxt = @"SELECT PRODUCT_ID,
PRODUCT_DESC,
UNIT_PRICE
FROM WAREHOUSE
WHERE PRODUCT_ID = :P_Product_ID";
using (OracleConnection conn = new OracleConnection(connstr))
using (OracleCommand cmd = new OracleCommand(cmdtxt, conn))
{
conn.Open();
cmd.CommandType = CommandType.Text;
cmd.CommandText = cmdtxt;
cmd.Parameters.Add(new OracleParameter(":P_Product_ID", OracleDbType.Int64)).Value = TB_Product_ID.Text;
OracleDataReader oraReader = cmd.ExecuteReader();
while (oraReader.Read())
{
ItemCount++;
RowCountLabel.Text = ItemCount.ToString();
DataGridViewRow dgvRow = new DataGridViewRow();
if (DGV_INVOICE.Rows.Count > 0)
{
foreach (DataGridViewRow ItemRow in DGV_INVOICE.Rows)
{
//Check if the product Id exists with the same Price
if (Convert.ToString(ItemRow.Cells[2].Value) == TB_Product_ID.Text)
{
//Update the Quantity of the found row
ItemRow.Cells[5].Value = Convert.ToString(1 + Convert.ToInt64(ItemRow.Cells[5].Value));
ProductIDExist = true;
}
else if (!ProductIDExist)
{
//Add the row to grid view
//dgvRow.Cells.Add(new DataGridViewCheckBoxCell()); //Edit_Checkbox index 0
dgvRow.Cells[0].Value = false;
//dgvRow.Cells.Add(new DataGridViewTextBoxCell()); //ItemCount index 1
dgvRow.Cells[1].Value = ItemCount;
//dgvRow.Cells.Add(new DataGridViewTextBoxCell()); //DGV_PRODUCT_ID index 2
dgvRow.Cells[2].Value = oraReader.GetValue(0);
//dgvRow.Cells.Add(new DataGridViewTextBoxCell()); //DGV_PRODUCT_DESC index 3
dgvRow.Cells[3].Value = oraReader.GetString(1);
//dgvRow.Cells.Add(new DataGridViewTextBoxCell()); //DGV_UNIT_PRICE index 4
dgvRow.Cells[4].Value = oraReader.GetValue(2);
// dgvRow.Cells.Add(new DataGridViewTextBoxCell()); //DGV_QUANTITY index 5
dgvRow.Cells[5].Value = "1";
// dgvRow.Cells.Add(new DataGridViewTextBoxCell()); //DGV_DISCOUNT index 6
dgvRow.Cells[6].Value = "0";
//dgvRow.Cells.Add(new DataGridViewTextBoxCell()); //DGV_TOTAL_PRICE index 7
//dgvRow.Cells[7].Value = "0";
//dgvRow.Cells.Add(new DataGridViewTextBoxCell()); //DGV_NOTES index 8
//dgvRow.Cells[8].Value = "-";
DGV_INVOICE.Rows.Add(dgvRow);
dgvRow.Selected = true;
}
}
}
else
{
//Add the row to grid view for the first time
dgvRow.Cells.Add(new DataGridViewCheckBoxCell()); //Edit_Checkbox index 0
dgvRow.Cells[0].Value = false;
dgvRow.Cells.Add(new DataGridViewTextBoxCell()); //ItemCount index 1
dgvRow.Cells[1].Value = ItemCount;
dgvRow.Cells.Add(new DataGridViewTextBoxCell()); //DGV_PRODUCT_ID index 2
dgvRow.Cells[2].Value = oraReader.GetValue(0);
dgvRow.Cells.Add(new DataGridViewTextBoxCell()); //DGV_PRODUCT_DESC index 3
dgvRow.Cells[3].Value = oraReader.GetString(1);
dgvRow.Cells.Add(new DataGridViewTextBoxCell()); //DGV_UNIT_PRICE index 4
dgvRow.Cells[4].Value = oraReader.GetValue(2);
dgvRow.Cells.Add(new DataGridViewTextBoxCell()); //DGV_QUANTITY index 5
dgvRow.Cells[5].Value = "1";
dgvRow.Cells.Add(new DataGridViewTextBoxCell()); //DGV_DISCOUNT index 6
dgvRow.Cells[6].Value = "0";
//dgvRow.Cells.Add(new DataGridViewTextBoxCell()); //DGV_TOTAL_PRICE index 7
//dgvRow.Cells[7].Value = "0";
//dgvRow.Cells.Add(new DataGridViewTextBoxCell()); //DGV_NOTES index 8
//dgvRow.Cells[8].Value = "-";
DGV_INVOICE.Rows.Add(dgvRow);
dgvRow.Selected = true;
}
}
}
}