代码之家  ›  专栏  ›  技术社区  ›  Scott Vercuski

将具有子对象作为属性的对象绑定到DataGrid

  •  8
  • Scott Vercuski  · 技术社区  · 16 年前

    我正在处理一个包含子对象的对象(见下面的示例)。我正试图绑定 List<rootClass> 到数据报。当我绑定 List<> ,在包含 subObject ,我看到以下值 ... "namespace.subObject" ... 字符串值显示正确。

    理想情况下,我希望看到 子对象 在数据单元中。我怎么画 subObject.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; } }
    }
    
    4 回复  |  直到 6 年前
        1
  •  9
  •   Lasse V. Karlsen    6 年前

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

    是否尝试绑定到value1.description?(我猜不起作用)。

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

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

    https://gist.github.com/lassevk/64ecea836116882a5d59b0f235858044

    使用:

    List<rootClass> yourList = ...
    TypedListWrapper<rootClass> bindableList = new TypedListWrapper<rootClass>(yourList);
    bindableList.BindableProperties = "Value1;Value2.Description;Value3.Description";
    gridView1.DataSource = bindableList;
    

    基本上,您绑定到 TypedList<T> 而不是 List<T> ,并调整BindableProperties属性。我在工作中做了一些更改,包括一个在运行时自动构建可绑定属性的更改,但它还没有在主干中。

    还可以添加计算属性,如:

    yourList.AddCalculatedProperty<Int32>("DescriptionLength",
        delegate(rootClass rc)
        {
            return rc.Value2.Description.Length;
        });
    

    或使用.NET 3.5:

    yourList.AddCalculatedProperty<Int32>("DescriptionLength",
        rc => rc.Value2.Description.Length);
    
        2
  •  9
  •   Marc Gravell    16 年前

    自从你提到 DataGridViewColumn (标签),我假设您是指WinForms。

    访问子属性是一件痛苦的事情;货币管理器绑定到列表,因此默认情况下您只能访问即时属性;但是,您可以克服此问题。 如果你绝对需要的话 使用自定义类型描述符。您也需要使用不同的令牌,比如“foo-bar”而不是“foo.bar”。然而,这是一个 专业 需要了解的工作量 PropertyDescriptor , ICustomTypeDescriptor 而且可能 TypeDescriptionProvider 几乎肯定不值得,

    最简单的修复方法是将属性公开为填充/传递:

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

    然后绑定到“value2description”等。

        3
  •  3
  •   M4N    16 年前

    我不确定您是否在使用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>
    
        4
  •  1
  •   GarethPN    13 年前

    不知道你是不是在找这样的东西…

    您可以编写如下方法:

    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;
    }
    

    然后像这样绑定对象:

    <asp:Literal Text='<%# getSubObject(Eval("Value2")) %>' runat="server" />