日期:2014-05-18  浏览次数:20503 次

asp.net 中如何绑定实体集合?
如题:

  用IList 还是 ArrayList 或者 List

  想给DataList或GridView绑定,不明白如何使用?

  希望能给出具体的例子。


------解决方案--------------------
用ArrayList 吧
------解决方案--------------------
ArrayList 应该是可以的
另两个,看样子也能行
------解决方案--------------------
http://blog.csdn.net/weifan_1/archive/2007/10/08/1815281.aspx

------解决方案--------------------
ArrayList应该可以直接作为数据源进行帮定
------解决方案--------------------

<asp:GridView ID="GV1" runat="server" OnRowDataBound="GV1_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Literal ID="litStr" runat="server"></asp:Literal>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>


CS:

protected void Page_Load(object sender, EventArgs e)
{
ArrayList al1 = new ArrayList();
al1.Add("111");
al1.Add("222");
al1.Add("333");
this.GV1.DataSource = al1;
this.GV1.DataBind();
}
protected void GV1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Literal litstr = (Literal)e.Row.FindControl("litStr");
litstr.Text = ((ArrayList)GV1.DataSource)[e.Row.RowIndex].ToString();
}
}
------解决方案--------------------
List吧,用泛形不用装箱拆箱.和Dataset用法一样,把集合中对象的属性名当成数据库中的字段名
------解决方案--------------------
up
------解决方案--------------------
Public Class personal

Private _personalName As String

Public Property personalName() As String
Get
Return _personalName
End Get
Set(ByVal value As String)
_personalName = value
End Set
End Property

Sub New(ByVal personalName As String)
Me._personalName = personalName
End Sub

End Class

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

'gv 是 gridview

gv.DataSource = createDataSource()
gv.DataBind()

End Sub

Public Function createDataSource() As System.Collections.Generic.List(Of personal)

Dim personals As New System.Collections.Generic.List(Of personal)
personals.Add(New personal("asp.net"))
personals.Add(New personal("vb.net"))
Return personals

End Function
------解决方案--------------------
用IList<T>泛型集合接口
------解决方案--------------------
VB的泛型用圆括号,还要加Of,不爽
------解决方案--------------------
恭喜~恭喜~
------解决方案--------------------
建议使用IList<T>
C# code

int pageSize = 10;
ISort sort = null;
long totalRecords;       
//调用方法获取数据集合
 IList<DesignProxy> designProxyList = service.QueryDesignProxyList(pageIndex, filterString.ToString(), sort, out totalRecords);
this.gvwResult.DataSource = designProxyList;
this.PalResult.Visible = true;
this.gvwResult.DataBind();  
......