结合自定义列表属性的DataGridView [英] Binding Custom List Property To DatagridView

查看:102
本文介绍了结合自定义列表属性的DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题是难以解释的。基本上我有一定的类,我们可以调用MyObj中的列表。一个此对象的属性是一个自定义列表本身。我想这个列表绑定到一个DataGridView,并有这个特殊的属性,也是一个列表中显示出来。有任何想法吗?我是不是足够清晰? :-P ..

I have a problem that is difficult to explain. Essentially I have a list of a certain class we can call MyObj. One of the properties of this object is a custom list itself. I would like to bind this List to a dataGridView and have this particular property that is also a list show up. Any ideas? Am I being clear enough? :-P..

下面是观念。我有我自己的自定义列表对象覆盖ToString()方法:

Here is the idea. I have my own custom list object overriding the ToString() method:

public class CategoriesList : List<Category>  
{  
  public override string ToString()
  {...}  
}

这是作为属性的对象,如:

This is used as a property in an object such as:

public MyObj  
{  
  public string Property1 {get; set; }  
  public string Property2 {get; set; }  
  public CategoriesList Categories {get; set; }  
} 

在反过来,我有这些对象如列表:

In turn, I have a list of these objects such as:

List<MyObj> myDataSouce = SomeRepository.GetMyObjList(); 



我在哪里绑定这一个数据网格视图:

Where I bind this to a datagrid view:

MyDataGridView.DataSource = myDataSource; 



Property1和Property2自动生成。有什么办法能有CategoriesList属性添加呢?我以前认为重写一个类的ToString()方法就足够了。

Property1 and Property2 are automatically generated. Is there any way to have the CategoriesList property be added as well? I previously thought Overriding the ToString() method on a class would be enough..

我真的失去了这一个,因为我不知道如何,甚至谷歌它:-P

I am really lost on this one as I have no idea how to even google for it :-P

推荐答案

假设你想在DataGridView显示代替列表的特定值,你'会希望使用自定义类型转换器。 。否则,你就需要放置在支持列表中的datagridview的列控制,如一个下拉列表并绑定到

Assuming that you'd like to display a specific value in place of the list in the datagridview, you'll want to use a custom TypeConverter. Otherwise you'll need to place a control in the datagridview column that supports lists, like a drop down list and bind to that.

对于前者:

基本上具有自定义类型转换器装点你的类别属性:

Basically decorate your categories property with a custom typeconverter:

[TypeConverter(typeof(MyConverter))] 
public CategoriesList Categories { get; set; }



然后使用一个自定义类型转换器,基本上告诉DataGrid中,当它遇到的类别属性是什么显示:

Then use a custom type converter that basically tells the datagrid that when it encounters the categories property what do display:

public class MyConverter : TypeConverter
{

    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
    {
        if (value is CategoriesList) {
            return value.ToString();
        }
        return base.ConvertFrom(context, culture, value);
    }
}

您需要添加您的列数据绑定手动添加绑定列并指定DataPropertyName为属性映射到该列,在这种情况下,类别

You'll need to add your column to be databound manually by adding an unbound column and specify the DataPropertyName for the property to be mapped to that column, in this case "Categories"

如果你正在寻找显示第二级性能以及那么这可能会有帮助:
http://blogs.msdn.com/b/msdnts/archive/2007/01/19/how -to-绑定-A-datagridview的列到一个秒级属性的一-数据source.aspx

If you're looking to display second level properties as well then this may help: http://blogs.msdn.com/b/msdnts/archive/2007/01/19/how-to-bind-a-datagridview-column-to-a-second-level-property-of-a-data-source.aspx

这篇关于结合自定义列表属性的DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆