gpt4 book ai didi

c# - DataGridView 列表 列?

转载 作者:行者123 更新时间:2023-11-30 21:20:01 24 4
gpt4 key购买 nike

我有一个绑定(bind)到列表的 datagridView。此列表由我的类组成,其中包含 2 个公共(public)属性、一个字符串名称和另一个列表自定义列表。见下文:

public class MyClass2
{
public string Name
{ get; set;}


public string Description
{
get;
set;
}
}

public class MyClass
{
List<MyClass2> myList;

public string Name
{
get;
set;
}

public List<MyClass2> CustomList
{
get { return myList ?? (myList= new List<MyClass2>()); }
}

}

然后在我的设计师页面中:

List<MyClass> myClassList = new List<MyClass>();
dataGridView.DataSource = myClassList;

现在,网格中唯一出现的列是 MyClass:Name 列,而 CustomList 列没有出现。我想要的是 CustomList 列来显示和显示类似 “Collection” 的内容,并显示“...”按钮,并在单击它时弹出“Collection Editor” .

有谁知道这是否可行以及如何启用它?如果有教程或任何可以帮助我的东西,我也会很感激。谢谢。

最佳答案

我认为使用泛型是一个干净的解决方案:

public class Sorter<T>: IComparer<T>
{
public string Propiedad { get; set; }

public Sorter(string propiedad)
{
this.Propiedad = propiedad;
}

public int Compare(T x, T y)
{
PropertyInfo property = x.GetType().GetProperty(this.Propiedad);
if (property == null)
throw new ApplicationException("El objeto no tiene la propiedad " + this.Propiedad);
return Comparer.DefaultInvariant.Compare(property.GetValue(x, null), property.GetValue(y, null));
}
}

使用示例:

string orderBy = "propertyName";
bool orderAsc = true;

List<MyExampleClass> myClassList = someMethod();

if (!string.IsNullOrEmpty(orderBy))
{
myClassList.Sort(new Sorter<MyExampleClass>(orderBy));
if (!orderAsc) myClassList.Reverse();
}

关于c# - DataGridView 列表 <T> 列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3506326/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com