【问题标题】:Select Items that Do Not StartsWith选择不以开头的项目
【发布时间】:2019-02-18 16:36:36
【问题描述】:

我的 WinForm (Visual Studio 2017) 有问题。

在我认为给你一些关于它的细节可能会使我们所有人受益之前。我只会发布我认为与问题相关的细节,所以如果你认为我遗漏了一些东西,请随时告诉我。还问我是否没有正确解释某些部分。 我使用DataTableReader.GetSchemaTable 方法来做一些事情,如果这完全相关的话。

我希望将列表的元素显示在 Textbox 中,然后将其复制到文本文件 ecc ecc 中。在Textbox 上方,我创建了一个DataGrid,您可以在其中看到NameFields,并且有一个名为“Able”的复选框,用于确定这些字段是否将在下面的Textbox 中显示(选中)(未选中)。

首先,我创建了一个类,在其中设置我想要在集合中的属性,例如名称和条件“Able”。我默认将其设置为 true(此处未显示),因此对于所有 NameFields,DataGridView 中的勾号当前已被选中。这意味着它们将显示在下面的Textbox 中,准备好被“filetexted”。

public class Info {
    public string NameField {get; set;}
    public bool Able {get; set;}
}

然后在另一个类中,我创建了一个 Observable Collection,它将填充上面创建的那些 NameFields(使用来自 SqlDataAdapter 的函数 Fill,我不会在这里展示)。

public class Do {
    public ObservableCollection<Info> Projects = new ObservableCollection<Info>();
}

最后,我对该集合中的元素进行了排序,以便首先显示以某些字母开头的元素(另一位用户帮助我完成了这个)。

var change = Projects.OrderByDescending(c => 
    c.NameField.StartsWith("OS")).ToList();
Projects.Clear();
foreach (Info aInfo in change) {
    Projects.Add(aInfo);
}

现在我需要的是,同一集合中不以这些字母开头的所有元素都将禁用对 Able 的检查。所以这意味着DataGrid 将在“Able”下勾选他的勾号,而那些精确的NameFields 不会出现在TextBox 中。

我在这个问题上遇到了真正的问题,我似乎找不到解决方案,所以我问你们。提前谢谢你。

【问题讨论】:

  • 简单 : c => !c.NameField.StartsWith("OS") 我加了 not !
  • 您是否保存了Info 类的所有实例的完整列表?
  • 这里不是很清楚你想要什么。如果您只想添加名称字段以“OS”开头的元素并忽略不包含的元素 - 为什么不只选择您想要的元素。或者,使用 foreach 循环遍历每个元素并根据名称字段的开头设置 Able 标志。
  • ObservableCollection 旨在供 WPF 使用。 BindingList 本来是供 WinForms 使用的。
  • 很抱歉我的回复迟了,但我还在做另一个项目,这让我很忙。 @jdweng 感谢您的回复,但我忘了提及我已经知道该功能的“反向”,我遇到的问题是我不知道如何放置控件(可能是 IF)以便选择那些没有开始的......

标签: c# winforms linq boolean observablecollection


【解决方案1】:

以下是有关如何对集合进行排序的建议,以便以 您的排序/搜索条件列在顶部,因此元素中的所有 .Able 字段 满足排序/搜索条件的设置为 true,而其余设置为 false。

代码清单包括 Info 对象的类,该类具有对集合进行排序和更新的方法。最后是一个带有方法的类来测试它是否可以正常工作。 我希望下面的代码清单有足够的注释以使其易于解释。

using System.Linq;
using System.Diagnostics;
using System.Collections.ObjectModel;


namespace TestSpace
{
    public class Info //Your original class for the project info objects.
    {
        public string NameField { get; set; }
        public bool Able { get; set; }
    }

    public class ProjectSorter  //COMMENT: Renamed your "Do" class to "ProjectSorter" since it seems more understandable.
    {
        // COMMENT:
        // In this proposed solution the SortProjects method is changed so that it now takes two parameters.        
        // The parameters are the project collection to be sorted, and the sort/search criteria.The procject  
        // collection to be sorterd is sent by ref so that any changes to it is reflected back to the caller.
        // (See https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/ref for more
        // information on passing objects by ref.)

        public void SortProjects(ref ObservableCollection<Info> projectCollectionToSort, string sortCriteria)
        {
            // As allready solved this will sort the collection based on the sort criteria into a new list.
            var updatedProjectList =
            projectCollectionToSort.OrderByDescending(c => c.NameField.StartsWith(sortCriteria)).ToList();

            // Using the list's .ForeEach method we iterate through the new list and uses a lambda expression
            // to set .Able to true or false based on the sort/search criteria.
            updatedProjectList.ForEach(c => {
                if (c.NameField.StartsWith(sortCriteria)) { c.Able = true; } else { c.Able = false; }
            });

            // We then reset our collection to a new ObservableCollection<Info> and fills it with the new 
            // sorted and updated list.
            projectCollectionToSort = new ObservableCollection<Info>(updatedProjectList);

            // Work done.
        }
    }

    public static class TestTheCode
    {
        // Method to test the ProjectSorter.SortProjects() method.
        public static void RunSortCollectionTest()
        {
            ProjectSorter projectSorter = new ProjectSorter();

            // Just for the purpose of this example an example collection is populated
            // here with some data to work with.
            // As you describe for your list the default value for Able is set to true.
            ObservableCollection<Info> projects = new ObservableCollection<Info>()
            {
                new Info { NameField="PER", Able = true},
                new Info { NameField="ALEX", Able = true},
                new Info { NameField="OSCAR", Able = true},
                new Info { NameField="ANDY", Able = true}
            };

            // We sort the collection "projects" by sending it by ref to the projectSorter.SortProjects()
            // method, together with the sort/search criteria.
            projectSorter.SortProjects(ref projects, "OS");

            // To display that the collection "projects" are now sorted as intended, and that all the
            // .Able fields are satt correctly true or false, we iterate through the projects collection 
            // and print the values for NameField and Able to the Output window (debug).
            foreach (Info aInfo in projects)
            {
                Debug.Print(aInfo.NameField + "  -->  " + aInfo.Able);
            }
        }
    }
}

要运行测试,只需从您的代码中调用 TestTheCode.RunSortCollectionTest()

【讨论】:

  • 感谢您的回答,但我也想解释一下您选择的编码,尤其是您将Able = false 放在所有这些领域的原因。如果我可以问,您是否测试过这段代码?
  • @MadSkunk,感谢您的意见。我现在对此进行了更多思考并重写了我的建议。当然,代码是经过测试的。我总是测试我的代码,不是吗? ;) 我希望代码清单的注释足以使其自我解释。但如果有任何问题或cmets,请分享。
  • @Simone,感谢您的意见/问题,请参阅上面的评论。
猜你喜欢
  • 1970-01-01
  • 2022-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-10
  • 2011-05-29
  • 1970-01-01
  • 2013-01-07
相关资源
最近更新 更多