【问题标题】:How do I make sure each selected cell has same value?如何确保每个选定的单元格具有相同的值?
【发布时间】:2014-05-14 21:28:13
【问题描述】:

我正在使用 DevExpress XAF。我有一个列表视图。在列表视图中,用户可以选择多行。我需要确保特定列/属性的所有单元格都保持相同的值。否则我需要抛出异常。

例如,如果用户选择 3 行,我需要确保 Trucker 列中的 3 个值相同。到目前为止,这是我的代码:

 if (lr.PurchaseLoad.Trucker != null)
 {
     if (lr.PurchaseLoad.Trucker.Name == lr.PurchaseLoad.Trucker.Name == true)//Is this correct?
     {

             // What am I comparing here?

     }
   else if (lr.PurchaseLoad.Trucker.Name.Equals(lr.PurchaseLoad.Trucker.Name) == false)
    {
    throw new UserFriendlyException("Please make sure your selected records have the same Trucker assigned.");                       
    }


  }

【问题讨论】:

    标签: c# listview devexpress xaf


    【解决方案1】:

    尝试这样的事情,使用 LINQ(假设 lr 是你的 ListView):

    var distinctNames = lr.SelectedItems.Cast<PurchaseLoad.Trucker>()
                                        .Where(x => x != null)
                                        .Select(x => x.Name)
                                        .Distinct()
                                        .Count();
    
    if (distinctNames == 0)
        throw new UserFriendlyException("Make at least one selection.");
    else if (distinctNames > 1)
        throw new UserFriendlyException("Please make sure your selected records have the same Trucker assigned.");
    

    我没有使用过DevExpress 控件。看起来您可能必须使用以下方法调整上述内容:

    ... = lr.SelectedObjects.Cast<PurchaseLoad.Trucker>()...
    

    在您的原始代码中,您的语法是关闭的。您使用以下方法比较两个值:

    if (value1 == value2)
    
    if (value1 != value2)
    

    不是:

    if (value1 == value2 == true)
    
    if (value1 == value2 == false)
    

    最终工作解决方案(来自 EB 的评论):

    foreach (LoadRelationship trucker in View.SelectedObjects)
    {
        var distinctNames =
            View.SelectedObjects.Cast<LoadRelationship>()
                                .Where(x => x != null)
                                .Select(x => x.PurchaseLoad.Trucker.Name)
                                .Distinct()
                                .Count();
    
        if (distinctNames > 1)
            throw new UserFriendlyException(
                "Please make sure your assigned Truckers are the same.");
    }
    

    【讨论】:

    • 感谢格兰特。这行得通。我确实做了一些细微的改变。但我得到了我想要的结果。我感谢您的帮助!这是工作代码:foreach (LoadRelationship trucker in View.SelectedObjects) { var distinctNames = View.SelectedObjects.Cast&lt;LoadRelationship&gt;() .Where(x =&gt; x != null) .Select(x =&gt; x.PurchaseLoad.Trucker.Name) .Distinct() .Count(); if (distinctNames &gt; 1) throw new UserFriendlyException("Please make sure your assigned Truckers are the same."); }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多