【问题标题】:Call a method if all rows of a specific datagridview column have same value如果特定 datagridview 列的所有行都具有相同的值,则调用方法
【发布时间】:2017-06-04 22:05:34
【问题描述】:

谁能告诉我如何检查 datagridview 的特定列的所有行是否具有相同的值。

我有一张名为“Courier”的表,其中包含多批货物。每个货件都有一个 shipping_state 列。 “Courier”表还有一个名为 courier_state 的列。 如果所有shipmment_state = 已处理,我想设置 courier_state = 已处理。

我试过了。

foreach(DatagridViewRow rw in dtgridshipment.Rows)
{
 string state = rw.Cells[dtgridshipment.Columns["shipment_state"].Index].Value.ToString();

if(state.Equals("Processed"))
{
  NShipment.UpdateStatus() //Update the status of courier_status with stored procedure
}
}

问题在于,如果我有一个“已处理”值的单行,则 courier_state 会更新为已处理。 如果 shipping_state 的所有行都已处理,我需要设置 courier_state = 已处理。

希望有人可以帮助我。提前致谢

【问题讨论】:

    标签: c# loops for-loop datagridview foreach


    【解决方案1】:

    因此,如果state.Equals("Processed") 在任何迭代中失败,则无需更新,这意味着如果条件失败,您可以停止迭代。所以你可以尝试这样的事情:

    bool canUpdate = true;
    
    foreach(DatagridViewRow rw in dtgridshipment.Rows)
    {
        string state = rw.Cells[dtgridshipment.Columns["shipment_state"].Index].Value.ToString();
    
        if(!state.Equals("Processed"))
        {
           // Need not to update the since state is not Processed for this row
           // Stop iteration here
           canUpdate = false;
           break;
        }
    }
    // value of canUpdate remains true if shipment_state for all rows are Processed
    // So we check that condition here and call the update
    if(canUpdate)
    {
        NShipment.UpdateStatus();
    }
    

    【讨论】:

    • 非常感谢。你太棒了。
    猜你喜欢
    • 2012-02-28
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 2022-10-09
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    相关资源
    最近更新 更多