ASP.NET-GridView数据绑定的几种方法

前提:页面需要显示多个表中的字段,几个表之间通过字段保持联系

方法一:直接进行级联查询,并将结果集通过SqlDataAdapter填充DataSet,之后将DataTable的DataView绑定到GridView的数据源,:

//连接数据库,并获得返回结果;

public dataset GetResult(string str1)

{

String str=“数据库连接字符串”;

string str1=“级联查询语句”

SqlConneciton sconnection = new SqlConnection(str);

SqlCommand scommand = new scommand();

scommand。connection = sconnection;

scommand。textcommand = str1;

sqlDataAdapter adapter = new sqlDataAdapter();

adapter。 selectcommand = scommand;

DataSet ds = new DataSet();

adapter。fill(ds);

return ds;

}

页面层.CS代码Fragment:

protected void Page_Load()

{

this.GridView1.datasource = ds.tables[0].defaultView;

this.GridView1.databind();

}

页面层。aspx文件代码Fragment1:将dataview内的所有字段,按照dataview的次序全部绑定到页面。

<asp:GridView id="GirdView1"></asp:GridView>

----------------------------------------------------------------

页面层1。aspx文件代码Fragment2:只绑定部分字段,并更换dataview的顺序规则:比如在dataview内name列显示在第三位,id显示在第二位;而在页面中要求不显示id列,而只显示name列,并排列在第二位:

<asp:GridView id=“GridView1”>

<asp:templateField>

   <itemTemplate>

       <%# ((datarowview)container。dataitem)[ "name" ]。toString() %>

    </ItemTemplate>

</asp:templateField>

<asp:templateField>

   <itemTemplate>

          <%# databinder.Eval(container。Dataitem,"name") %>

   </itemTemplate>

</asp:templateField>

<asp:templateField>

   <itemTemplate>

          <%# databinder.Eval(container,"Dataitem。name") %>

   </itemTemplate>

</asp:templateField>

</asp:GridView>

------------------------------------------------------------------------------------------------------------------------------

方法二:先取出A张表内所需要显示的记录,逐一赋值到n个info对象内,将所有的info对象保存在一个集合类内,(例如:List<>,dictionary,collection,arraylist,hashtable,注意这些类都是非线程安全的),之后将此集合类绑定到GirdView内。

但是一个GirdView只能绑定一个集合类,由于缺少其他表内的字段,我们必须通过A表内的ID,查询B表内的name。所以我们需要在页面层的CS文件,bll层,以及Dal层加入查询name的方法:

由于Asp。net可以绑定方法,属性,控件属性,等等,所以这种做法是行得通的:

<asp:GridView id=“GridView1”>

<asp:templateField>

   <itemTemplate>

       <%# GetName(((XXXinfo)container。dataitem)。id 。tostring())%>

    </ItemTemplate>

</asp:templateField>

<asp:templateField>

   <itemTemplate>

          <%# GetName(databinder.Eval(container。Dataitem,"name")) %>

   </itemTemplate>

</asp:templateField>

<asp:templateField>

   <itemTemplate>

          <%#   GetName(databinder.Eval(container,"Dataitem。name") ) %>

   </itemTemplate>

</asp:templateField>

<asp:templateField>

   <itemTemplate>

          <%#   GetName(databinder.Eval(“name”) ) %>

   </itemTemplate>

</asp:templateField>

</asp:GridView>

//注:GetName方法被写在页面的。cs文件内,方法为空方法,return bll的GetName方法,之后再Return Dal的GetName方法。

------------------------------------------------------------------------------------------------------------------------------

总结:第二种方法的性能很差,因为我需要访问两次数据库,而且与方法一比较会多出很多方法。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值