你怎么能在.NET中对一个对象进行数据绑定?

时间:2020-12-30 22:44:47

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 ?

我想使用一个公开datasource属性的组件,但我不想使用整个对象列表提供数据源,我只想使用简单的对象。有没有办法做到这一点?

The mentioned component is DevExpress.XtraDataLayout.DataLayoutControl - this is fairly irrelevant to the question though.

提到的组件是DevExpress.XtraDataLayout.DataLayoutControl - 这与问题完全无关。

5 个解决方案

#1


8  

Databinding expects an IEnumerable object, because it enumorates over it just like a foreach loop does.

数据绑定需要一个IEnumerable对象,因为它就像foreach循环一样对它进行了枚举。

So to do this, just wrap your single object in an IEnumerable.

所以要做到这一点,只需将您的单个对象包装在IEnumerable中。

Even this would work:

即使这样也可行:

DataBindObject.DataSource = new List<YourObject>().Add(YourObjectInstance);

#2


1  

In ASP.NET2.0 you can use the generic collections to make this single object a list of only one object in it that you can databind to any server control using the objectdatasource, e.g.

在ASP.NET2.0中,您可以使用泛型集合使该单个对象成为其中只有一个对象的列表,您可以使用objectdatasource将数据绑定到任何服务器控件,例如,

List<clsScannedDriverLicense> DriverLicenses = new
List<clsScannedDriverLicense>();
//this creates a generic collection for you that you can return from
//your BLL to the ObjectDataSource
DriverLicenses.Add(TheOneObjectThatYouHaveofType_c lsDriverLicense);

Then your ObjectDataSource would look like that:

然后你的ObjectDataSource看起来像这样:

<asp:ObjectDataSource ID="odsDL" runat="server"
SelectMethod="OrdersByCustomer"
TypeName="YourBLL.UtiltiesClassName"
DataObjectTypeName="clsScannedDriverLicense">
</asp:ObjectDataSource>

Source

#3


1  

I don't think you have much choice other than using an class that implements IEnumerable<T>. Even if the DataSource property was smart enough to take a scalar object, it would probably convert it internally to a vector.

除了使用实现IEnumerable 的类之外,我认为你没有太多选择。即使DataSource属性足够智能以获取标量对象,它也可能将其内部转换为向量。

I would however consider using a simple array rather than a List<T> as this will result in fewer memory allocations. If you don't like the array syntax (and also to increase readability) you could use a helper method:

但是我会考虑使用一个简单的数组而不是List ,因为这会导致更少的内存分配。如果您不喜欢数组语法(以及提高可读性),可以使用辅助方法:

T[] DataSourceHelper::ToVector(T scalar) { return new T[] { scalar }; }

T [] DataSourceHelper :: ToVector(T标量){return new T [] {scalar}; }

#4


1  

I'm after the same thing you are. I've posted a new question Two-way databinding of a custom templated asp.net control that has a bit of a lead. See what you can make of it...

我跟你做同样的事。我发布了一个新问题自定义模板化asp.net控件的双向数据绑定,有点领先。看看你能做些什么......

#5


0  

Using this in my formView:

在我的formView中使用它:

databoundControl.DataSource = new [] { singleObject };
databoundControl.DataBind();

#1


8  

Databinding expects an IEnumerable object, because it enumorates over it just like a foreach loop does.

数据绑定需要一个IEnumerable对象,因为它就像foreach循环一样对它进行了枚举。

So to do this, just wrap your single object in an IEnumerable.

所以要做到这一点,只需将您的单个对象包装在IEnumerable中。

Even this would work:

即使这样也可行:

DataBindObject.DataSource = new List<YourObject>().Add(YourObjectInstance);

#2


1  

In ASP.NET2.0 you can use the generic collections to make this single object a list of only one object in it that you can databind to any server control using the objectdatasource, e.g.

在ASP.NET2.0中,您可以使用泛型集合使该单个对象成为其中只有一个对象的列表,您可以使用objectdatasource将数据绑定到任何服务器控件,例如,

List<clsScannedDriverLicense> DriverLicenses = new
List<clsScannedDriverLicense>();
//this creates a generic collection for you that you can return from
//your BLL to the ObjectDataSource
DriverLicenses.Add(TheOneObjectThatYouHaveofType_c lsDriverLicense);

Then your ObjectDataSource would look like that:

然后你的ObjectDataSource看起来像这样:

<asp:ObjectDataSource ID="odsDL" runat="server"
SelectMethod="OrdersByCustomer"
TypeName="YourBLL.UtiltiesClassName"
DataObjectTypeName="clsScannedDriverLicense">
</asp:ObjectDataSource>

Source

#3


1  

I don't think you have much choice other than using an class that implements IEnumerable<T>. Even if the DataSource property was smart enough to take a scalar object, it would probably convert it internally to a vector.

除了使用实现IEnumerable 的类之外,我认为你没有太多选择。即使DataSource属性足够智能以获取标量对象,它也可能将其内部转换为向量。

I would however consider using a simple array rather than a List<T> as this will result in fewer memory allocations. If you don't like the array syntax (and also to increase readability) you could use a helper method:

但是我会考虑使用一个简单的数组而不是List ,因为这会导致更少的内存分配。如果您不喜欢数组语法(以及提高可读性),可以使用辅助方法:

T[] DataSourceHelper::ToVector(T scalar) { return new T[] { scalar }; }

T [] DataSourceHelper :: ToVector(T标量){return new T [] {scalar}; }

#4


1  

I'm after the same thing you are. I've posted a new question Two-way databinding of a custom templated asp.net control that has a bit of a lead. See what you can make of it...

我跟你做同样的事。我发布了一个新问题自定义模板化asp.net控件的双向数据绑定,有点领先。看看你能做些什么......

#5


0  

Using this in my formView:

在我的formView中使用它:

databoundControl.DataSource = new [] { singleObject };
databoundControl.DataBind();