问题描述
如果我有以下ASP.NET code(它没有完成 - 显然有很多缺失,但没有它的问题):
If I have the following ASP.NET code (it's not complete - obviously there's a lot missing, but none of it matters):
<asp:GridView>
<Columns>
<asp:TemplateField>
<ItemTemplate>
My Label: <asp:Label />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
My Text Box: <asp:TextBox />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
如果我在codeBehind是这样的:
And if I have something like this in the CodeBehind:
Private MyListOfObjects As List(Of MyObject)
...
Public Class MyObject
Public MyTextBoxString as String
Public MyLabelString as String
End Class
我如何可以将绑定在GridView,这样一行相当于我的 MyListOfObjects
列表中的一个项目,这样数据将填充并在页面加载或回传坚持?我从来没有做过的自定义数据绑定像在此之前,这样一个完整的解释将是非常有益的。所有的教程我已经遇到到目前为止只说说使用直接与GridView的数据库查询结果,这不是我所需要的。
How can I bind the GridView so that one row is equivalent to one item in my MyListOfObjects
list, and so that the data will populate and persist across page loads or postbacks? I've never done custom databinding like this before, so a full explanation would be very helpful. All the tutorials I've come across so far only talk about using GridViews directly with Database query results, and that's not what I need.
谢谢!
推荐答案
就在GridView的数据源设置为你的对象。
Just set the datasource of the gridview to your object.
MyGridView.DataSource = myList
MyGridView.DataBind()
下面是一个非常相似职位:
Here's a very similiar post:
Binding返回列表℃的方法;员工&GT;到GridView
看起来你正在使用vb.net列表。请记住列表可以容纳整数,字符串,日期,对象(其中包括用户自定义类型(你的对象))。
所以,你可以在数据源属性设置为列表绑定一个GridView到一个列表对象。
Looks like you are using a list in vb.net. Remember lists can hold integers, strings, dates, objects (these include user defined types (your object)).So you can bind a gridview to a list object by setting the datasource property to your list.
在上面的例子中,myList中,可能拥有一吨的员工对象,等于是将其分配给数据源和 .DataBind()
瞧一个gridview每包含你的对象行。
In the above example, myList, might hold a ton of employee objects, etc. So assign it to the datasource and .DataBind()
and voila a gridview with each row containing your object.
这篇关于我如何绑定一个GridView到自定义对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!