Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将具有子对象的对象作为属性绑定到datagrid_C#_Datagrid_Binding_Object_Datagridviewcolumn - Fatal编程技术网

C# 将具有子对象的对象作为属性绑定到datagrid

C# 将具有子对象的对象作为属性绑定到datagrid,c#,datagrid,binding,object,datagridviewcolumn,C#,Datagrid,Binding,Object,Datagridviewcolumn,我使用的对象中包含子对象(请参见下面的示例)。我正在尝试将列表绑定到datagrid。当我绑定列表时,在包含子对象的单元格中,我看到以下值。。。“namespace.subObject”…字符串值显示正确 理想情况下,我希望在数据单元中看到子对象的“Description”属性。如何将子对象.Description映射到数据单元中显示 public class subObject { int id; string description; public string Desc

我使用的对象中包含子对象(请参见下面的示例)。我正在尝试将
列表
绑定到datagrid。当我绑定
列表
时,在包含
子对象
的单元格中,我看到以下值
。。。“namespace.subObject”…
字符串值显示正确

理想情况下,我希望在数据单元中看到
子对象的“Description”属性。如何将
子对象.Description
映射到数据单元中显示

public class subObject
{
   int id;
   string description;

   public string Description
   { get { return description; } }
}

public class rootClass
{
   string value1;
   subObject value2;
   string value3;

   public string Value1
   { get { return value1; } }

   public subObject Value2
   { get { return value2; } }

   public string Value3
   { get { return value3; } }
}

如果我没有弄错的话,它会显示对子对象调用.ToString()的结果,因此您可以重写它以返回描述的内容

您是否尝试过仅绑定到Value1.Description?(我猜这是行不通的)

我有一个类,可以用来代替绑定时的列表,这将处理这个问题,它实现了ITypedList,它允许集合为其对象提供更多的“属性”,包括计算属性

我拥有的文件的最新版本如下:

使用:

List<rootClass> yourList = ...
TypedListWrapper<rootClass> bindableList = new TypedListWrapper<rootClass>(yourList);
bindableList.BindableProperties = "Value1;Value2.Description;Value3.Description";
gridView1.DataSource = bindableList;
List yourList=。。。
TypedListWrapper bindableList=新的TypedListWrapper(您的列表);
bindableList.BindableProperties=“Value1;Value2.说明;Value3.说明”;
gridView1.DataSource=bindableList;
基本上,您可以绑定到
TypedList
的实例,而不是
List
,并调整BindableProperties属性。我在工作中做了一些更改,包括一个只是在运行时自动构建BindableProperties的更改,但它还没有在主干中

您还可以添加计算属性,如下所示:

yourList.AddCalculatedProperty<Int32>("DescriptionLength",
    delegate(rootClass rc)
    {
        return rc.Value2.Description.Length;
    });
<asp:Literal Text='<%# getSubObject(Eval("Value2")) %>' runat="server" />
yourList.AddCalculatedProperty(“DescriptionLength”,
委托(rootClass rc)
{
返回rc.Value2.Description.Length;
});
或使用.NET 3.5:

yourList.AddCalculatedProperty<Int32>("DescriptionLength",
    rc => rc.Value2.Description.Length);
yourList.AddCalculatedProperty(“DescriptionLength”,
rc=>rc.Value2.Description.Length);

我不确定您是否正在使用ASP.NET,但如果是,则可以使用模板列和Eval()方法来显示嵌套对象的值。例如,要显示子对象的描述属性:

<asp:GridView ID="grid" runat="server" AutoGenerateColumns="true">
  <Columns>
    <asp:TemplateField>
      <ItemTemplate>
        <asp:Literal Text='<%# Eval("Value2.Description") %>' runat="server" />
      </ItemTemplate>
    </asp:TemplateField>
  </Columns>
</asp:GridView>

既然您提到了
DataGridViewColumn
(标记),我想您指的是winforms

访问子属性是一种痛苦;货币管理器绑定到列表,因此默认情况下您只能访问即时属性;但是,如果绝对需要,可以使用自定义类型描述符来克服此问题。您还需要使用不同的令牌,比如“Foo_Bar”而不是“Foo.Bar”。然而,这是一项主要的工作,需要了解
PropertyDescriptor
ICustomTypeDescriptor
和可能的
TypeDescriptionProvider
,几乎肯定不值得

最简单的修复方法是将属性公开为垫片/直通:

public string Value2Description {
    get {return Value2.Description;} // maybe a null check too
}

然后绑定到“Value2Description”等。

不确定您想要的是不是这样的东西

您可以编写如下方法:

protected string getSubObject(object o)
{
    string result = string.empty;

    try
    {
        result = ((subObject)o).Description;
    }
    catch
    { /*Do something here to handle/log your exception*/ } 

    return result;
}
然后像这样绑定对象:

yourList.AddCalculatedProperty<Int32>("DescriptionLength",
    delegate(rootClass rc)
    {
        return rc.Value2.Description.Length;
    });
<asp:Literal Text='<%# getSubObject(Eval("Value2")) %>' runat="server" />


仅供参考,ITypedList主要是1.1;在2.0中,您可以对基础类型(即列表中的T)使用
TypeDescriptionProvider
来执行相同的操作-pI将查看TypeDescriptionProvider,然后:)如果您查看
超描述符
代码(只需搜索),您可以快速获得介绍。一旦您了解了GetProperties(),就应该很熟悉了-普拉斯,我知道这个问题已经很老了,但是你是否仍然可以在网上找到来源?上面的url返回了一个500错误,我对TypedList代码非常感兴趣?这是DGV的缺点之一,我非常讨厌它,也是为什么我几乎总是绑定到匿名类型的IEnumerable。