根据产品datagridview的状态更改状态

时间:2017-07-30 09:39:24

标签: c# datagridview

我有这个订单表及其产品。它看起来像这样:

OrderID : 001            Order Status : ---------

Products Ordered Qty : 3

Products Ordered: (in datagridview)
Status       ProductName        QtyOrdered       QtyReceived
============================================================
Unreceived      p1                   2                0
Complete        p2                   3                3
Incomplete      p3                   4                4

现在它的订单状态应该根据datagridview中产品的状态而改变。

如果所有产品状态为"完成",订单状态=完成订单。 即使只有一个产品有"不完整",订单状态=后退订单。 默认情况下,所有产品均为Unreceived,订单状态为Unceceived。

这是我的代码。但它总是使订单状态="完成订单"。 顺便说一句,如果有帮助,我会将此代码放在Click DataGridView事件上。

 private void checkorderstatus()
    {
        bool incomplete = true;
        bool complete = true;

        if(dgvReceivedproducts.Rows.Count == Convert.ToInt32(rrQty.Text))
        {
            foreach (DataGridViewRow rw in dgvReceivedproducts.Rows)
            {
                string state = 
rw.Cells[dgvReceivedproducts.Columns["Status"].Index].Value.ToString();

                if (state.Equals("Unreceived"))
                {
                    incomplete = false;
                    break;
                }
                else if (state.Equals("Incomplete"))
                {
                    incomplete = true;
                }

                if (!state.Equals("Complete"))
                {

                    complete = false;
                    break;
                }

            }

            if (incomplete)
            {
                crud.AddRecord("Update Orders Set Status  = 'Back Order'  
where OrderID  = '" + rrorderid.Text + "'  ");

            }

            if (complete)
            {
                crud.AddRecord("Update Orders Set Status  = 'Complete Order'  
where OrderID  = '" + rrorderid.Text + "'  ");

            }
        }        
    }

1 个答案:

答案 0 :(得分:0)

请尝试这个,我认为它可以在不拆分代码的情况下工作......

private void checkorderstatus()
{
    bool Unreceived = true;
    bool complete = true;

    if (dgvReceivedproducts.Rows.Count == Convert.ToInt32(rrQty.Text))
    {
        foreach (DataGridViewRow rw in dgvReceivedproducts.Rows)
        {
            string state = rw.Cells[dgvReceivedproducts.Columns["Status"].Index].Value.ToString();

            if (!state.Equals("Complete"))
            {
                complete = false;
                if (!state.Equals("Unreceived"))
                {
                    Unreceived = false ;
                }
                break;
            }

        }


        if (complete)
        {
            crud.AddRecord("Update Orders Set Status  = 'Complete Order'  where OrderID = '" + rrorderid.Text + "'  ");

        }else if (Unreceived)
        {
            crud.AddRecord("Update Orders Set Status  = 'Unreceived'  where OrderID = '" + rrorderid.Text + "'  ");

        }else 
        {
            crud.AddRecord("Update Orders Set Status  = 'Back Order'  where OrderID = '" + rrorderid.Text + "'  ");


        }
    }
}