ASP.NET-GridView資料繫結的幾種方法

來源:互聯網
上載者:User

 

前提:頁面需要顯示多個表中的欄位,幾個表之間通過欄位保持聯絡

方法一:直接進行級聯查詢,並將結果集通過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可以Binder 方法,屬性,控制項屬性,等等,所以這種做法是行得通的:

<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方法。

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

總結:第二種方法的效能很差,因為我需要訪問兩次資料庫,而且與方法一比較會多出很多方法。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.