如何在.NET中数据绑定单个对象? [英] How can you databind a single object in .NET?
问题描述
我想使用一个暴露数据源属性的组件,而不是提供数据源与整个对象列表,我只想使用简单的对象。有没有办法这样做?
I would like to use a component that exposes the datasource property, but instead of supplying the datasource with whole list of objects, I would like to use only simple object. Is there any way to do this ?
所提到的组件是DevExpress.XtraDataLayout.DataLayoutControl - 这与问题相当无关。
The mentioned component is DevExpress.XtraDataLayout.DataLayoutControl - this is fairly irrelevant to the question though.
推荐答案
数据绑定需要一个IEnumerable对象,因为它像一个foreach循环一样枚举它。
Databinding expects an IEnumerable object, because it enumorates over it just like a foreach loop does.
所以要做到这一点,只需将单个对象包装在一个IEnumerable中。
So to do this, just wrap your single object in an IEnumerable.
即使这样也可以:
DataBindObject.DataSource = new List<YourObject>().Add(YourObjectInstance);
这篇关于如何在.NET中数据绑定单个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!