C#将对象数组转换为数据表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/525744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# convert object array into datatable
提问by George2
I failed to find a sample about how to convert an array instances of user defined types into a (C# ADO.Net) datatable -- I want to use the datatable to bind to ASP.Net data bound controls (e.g. gridview). Could anyone provide a sample or recommend me to some simple samples?
我没有找到有关如何将用户定义类型的数组实例转换为 (C# ADO.Net) 数据表的示例——我想使用数据表绑定到 ASP.Net 数据绑定控件(例如 gridview)。谁能提供样品或推荐我一些简单的样品?
Another question is, whether it is a must to convert to datatable in order to bound to controls in ASP.Net? Could I bound to any array of user defined types?
另一个问题是,是否必须转换为datatable才能绑定到ASP.Net中的控件?我可以绑定到任何用户定义类型的数组吗?
thanks in advance, George
提前致谢,乔治
回答by Marc Gravell
No, you don't need a DataTable
to do binding - in fact, most binding now is object based, especially with LINQ (which generates an object model, not a DataTable
). Just use the public property names (from your class) as the data-members for different columns, etc.
不,您不需要 aDataTable
进行绑定 - 事实上,现在大多数绑定都是基于对象的,尤其是使用 LINQ(它生成一个对象模型,而不是 a DataTable
)。只需使用公共属性名称(来自您的类)作为不同列的数据成员等。
回答by Sean Reilly
I would suggest using the ObjectDataSourceclass to do what you want. Then you won't need to use a DataTable at all, and you'll still be able to get data binding. ObjectDataSource allows you to expose user defined types to data binding, and it doesn't matter how the types are obtained; they don't need to come from a database at all, if that's what you need.
我建议使用ObjectDataSource类来做你想做的事。那么您将根本不需要使用 DataTable,并且您仍然能够获得数据绑定。ObjectDataSource 允许您将用户定义的类型暴露给数据绑定,并且如何获取类型并不重要;如果您需要,它们根本不需要来自数据库。
回答by miti737
I had the same requirement and I found a nice article at Pauls Blog. You can have a look at the following link if you still wanted it
我有同样的要求,我在 Pauls 博客上找到了一篇不错的文章。如果你仍然想要它,你可以看看下面的链接
http://weblogs.asp.net/psperanza/archive/2005/05/18/407389.aspx
http://weblogs.asp.net/psperanza/archive/2005/05/18/407389.aspx
Here is the code that I have used in my project. Hope that helps
这是我在项目中使用的代码。希望有帮助
Public Function ConvertArrayToDatatable(ByVal arrList As ArrayList) As DataTable
Dim dt As New DataTable
Try
If arrList.Count > 0 Then
Dim arrype As Type = arrList(0).GetType()
dt = New DataTable(arrype.Name)
For Each propInfo As PropertyInfo In arrype.GetProperties()
dt.Columns.Add(New DataColumn(propInfo.Name))
Next
For Each obj As Object In arrList
Dim dr As DataRow = dt.NewRow()
For Each dc As DataColumn In dt.Columns
dr(dc.ColumnName) = obj.GetType().GetProperty(dc.ColumnName).GetValue(obj, Nothing)
Next
dt.Rows.Add(dr)
Next
End If
Return dt
Catch ex As Exception
Return dt
End Try
End Function
回答by Crowley
In regards to the comment "can not use LINQ for non-technical reasons":
关于评论“由于非技术原因不能使用 LINQ”:
I presume you don't want to use LINQ to SQL since you want to maintain your three-tiered architecture. But you may not know that you can use LINQ to Objects and that would provide the flexibility you seek.
我认为您不想使用 LINQ to SQL,因为您想维护三层架构。但是您可能不知道您可以使用 LINQ to Objects,这将提供您所寻求的灵活性。
Suppose you have a middle tier method: List Tier2.SomeMethod(). You can assign that return value of that method as the GridView data source and then specify SomeObject methods/properties in the GridView control: gvSomeGridView.DatasSource = Tier2.SomeMethod(); gvSomeGridView.DataBind();
假设您有一个中间层方法:List Tier2.SomeMethod()。您可以将该方法的返回值指定为 GridView 数据源,然后在 GridView 控件中指定 SomeObject 方法/属性: gvSomeGridView.DatasSource = Tier2.SomeMethod(); gvSomeGridView.DataBind();
Further you can then retrieve SomeObject from the DataItem in GridView callbacks.
此外,您可以从 GridView 回调中的 DataItem 中检索 SomeObject。
HTH.
哈。