我的WinForm有问题(Visual Studio 2017)。
在我认为给你一些细节之前,我可以从中受益。我只会发布我认为与问题相关的细节,所以如果你认为我错过了什么,请随时告诉我。如果我没有正确解释某些部分,请问我。我使用DataTableReader.GetSchemaTable
方法来做事情,如果那是相关的。
我希望列表的元素能够在Textbox
中显示,然后将其复制到文本文件ecc ecc中。在Textbox
上方,我做了一个DataGrid
,你可以在其中看到NameField
s,并且有一个名为“Able”的复选框,用于确定这些字段是否将在下面的Textbox
中显示(选中)(未选中)。
首先,我创建了一个类,我在集合中设置了我想要的属性,例如Name和条件“Able”。我默认设置为true(此处未显示),因此对于所有NameField
s,当前检查DataGridView
中的刻度。这意味着它们将出现在下面的Textbox
中,准备好“filetexted”。
public class Info {
public string NameField {get; set;}
public bool Able {get; set;}
}
然后在另一个课程中,我创建了一个Observable Collection,它将填充上面创建的NameField
s(使用Fill
中的SqlDataAdapter
函数,我不会在这里展示)。
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”下打勾,而那些精确的NameField
s将不会出现在TextBox
中。
我遇到了这个问题的真正问题,我似乎无法找到解决方案,所以我问你们。先感谢您。
以下是对如何对您的集合进行排序以使列出以排序/搜索条件开头的元素在顶部列出的建议,以便满足排序/搜索条件的元素中的所有.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()
即可。