Как проверить, больше ли вставленное количество, чем новый запас в моей ячейке Datagridview - PullRequest
0 голосов
/ 11 июля 2020

Я хочу создать функцию, которая будет проверять, больше ли вставленное количество, чем новый запас в моей ячейке Datagridview ["New Stock"], чтобы предотвратить выполнение данных.

enter image description here введите описание изображения здесь

double qty,totalprice,discount;
double.TryParse(txtqty.Text, out qty);
double.TryParse(txttotalprice1.Text, out totalprice);
double.TryParse(txtdiscount.Text, out discount);


//Boolean to check if he has row has been
bool Found = false;
if (dataGridView1.Rows.Count > 0)
{

    // Check if the product Id exists with the same Price
    foreach (DataGridViewRow row in dataGridView1.Rows)
{

    if (Convert.ToString(row.Cells[0].Value) == cmbproductcode.Text && Convert.ToString(row.Cells[3].Value) == txtprice1.Text)
    {
        //Update the Quantity of the found row
        row.Cells[5].Value = Convert.ToString(qty + Convert.ToInt16(row.Cells[5].Value));
        row.Cells[7].Value = Convert.ToString(discount + Convert.ToInt16(row.Cells[7].Value)); //txtqty
        row.Cells[8].Value = Convert.ToString(totalprice + Convert.ToInt32(row.Cells[8].Value)); //txttotalprice                            
        Found = true;
        getprice();
    }

}
    if (!Found)
    {
        //Add the row to grid view                           
        getinfo();
    }

}
else
{
    //Add the row to grid view for the first time                   
    getinfo();

}  

1 Ответ

0 голосов
/ 11 июля 2020

 public double prev;
 private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
 {
            string cellValue;
            int i = e.RowIndex;
            cellValue = dataGridView1.Rows[i].Cells[1].Value.ToString();
            prev = Convert.ToDouble(cellValue);
 }
 private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
 {
            string cellValue;
            int i = e.RowIndex;
            cellValue = dataGridView1.Rows[i].Cells[1].Value.ToString();
            
            double cur = Convert.ToDouble(cellValue);
           if(isvalid(prev,cur))
            dataGridView1.Rows[i].Cells[1].Value = prev.ToString();
           else
                dataGridView1.Rows[i].Cells[1].Value = cellValue;
 }


 private Boolean isvalid(double pr,double cur)
 {
            return cur > pr ? false : true;
 }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...