我的WinForm(Visual Studio 2017)有问题。
在我想给您所有细节之前,我想一切可以让我们所有人受益。我只会发布我认为与问题相关的详细信息,因此,如果您认为我错过了某些信息,请随时告诉我。也请问我是否没有正确解释某些部分。
我用DataTableReader.GetSchemaTable
方法来做事情,如果那真的很重要的话。
我希望列表的元素显示在Textbox
中,然后将其复制到文本文件ecc ecc中。在Textbox
上方,我制作了一个DataGrid
,在其中您可以看到NameField
,并且有一个名为“ Able”的复选框,用于确定是否要在{中显示(选中)这些字段{1}}是否位于下面(未选中)。
首先,我创建了一个类,在该类中设置集合中所需的属性,例如Name和条件“ Able”。我默认将其设置为true(此处未显示),因此对于所有Textbox
,NameField
中的勾均已选中。这意味着它们将显示在下面的DataGridView
中,准备进行“文件文本化”。
Textbox
然后在另一个类中,我创建了一个Observable集合,其中将填充上面创建的public class Info {
public string NameField {get; set;}
public bool Able {get; set;}
}
(使用NameField
中的函数Fill
,在此不再显示)
SqlDataAdapter
最后,我对该集合中的元素进行了排序,以便首先显示以某些字母开头的元素(另一位用户帮助了我)。
public class Do {
public ObservableCollection<Info> Projects = new ObservableCollection<Info>();
}
现在我需要的是,同一集合中所有不以这些字母开头的元素都将禁用对Able的检查。因此,这意味着var change = Projects.OrderByDescending(c =>
c.NameField.StartsWith("OS")).ToList();
Projects.Clear();
foreach (Info aInfo in change) {
Projects.Add(aInfo);
}
的“ Able”下的勾号将被取消选中,并且精确的DataGrid
不会出现在NameField
中。
我在遇到这个问题时确实遇到了问题,但似乎找不到解决方法,所以我问你们。预先谢谢你。
答案 0 :(得分:0)
这里是关于如何对集合进行排序的建议,以便以 您的排序/搜索条件列在顶部,因此元素中的所有.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()
。