技巧百問(8):再談ASP.NET中DataGrid資料繫結以及分頁!

來源:互聯網
上載者:User
        DataGrid使用的很多,其中資料繫結以及分頁是個老問題,這裡我只是把我個人的一點經驗說一說,畢竟我還是新手。
        DataGrid綁定有很多種,可以用DataView,DataReader,DataSet這些來綁定,還可以使用自訂控制項來綁定,其實說穿了也就是資料對應顯示。而分頁也有很多種:1、使用sql語句,select top 10 from xxx where xxx,後面就加not in (select top 10 from xxx where xxx)。2、使用一次讀取資料,然後通過程式控制分頁。
下面是一個簡單分頁以及綁定的過程:

先建立DataGrid 1 <asp:DataGrid AllowPaging="true" AllowSorting="true" AutoGenerateColumns="false" Width="100%" OnPageIndexChanged="changepage"
2     FooterStyle-BackColor="#0099FF" ID="datagrid1" ItemStyle-BackColor="#006699" PagerStyle-NextPageText="下一頁" PagerStyle-PrevPageText="上一頁" PageSize="20" runat="server">
3     <columns>
4        <asp:BoundColumn HeaderText="使用者編號" HeaderStyle-Width="100" ItemStyle-Width="50" DataField="_userid"/>
5        <asp:BoundColumn HeaderText="使用者名稱" HeaderStyle-Width="100" ItemStyle-Width="100" DataField="id"/>
6        <asp:BoundColumn HeaderText="電子郵件" HeaderStyle-Width="150" ItemStyle-Width="150" DataField="email"/>
7        <asp:BoundColumn HeaderText="類型" HeaderStyle-Width="50" ItemStyle-Width="50" DataField="type"/>
8     </columns>
9     </asp:DataGrid>

其中AllowPaging是允許自動分頁,AllowSorting是允許自動排序,AutoGenerateColumns是允許自動產生列(如何要自己定義列這裡就要設為false),後面就是分頁的事件,PagerStyle-mode是分頁時候下面顯示的類型,預設是上一頁,下一頁那種,還有就是NumericPages,顯示的是12345那種類型。下面的columns中就是要顯示的列,後面DataField就是要綁定的欄位名。

然後就是寫Page_Load代碼,一開始就要綁定,所以就是查詢資料庫擷取資料

1 string sql="select _userid,id,type,email from user where city!='空' order by _userid desc";
2 string connstr="uid=sa;password=123;database=kkk;server=(local)";
3          SqlConnection conn=new SqlConnection(connstr);
4          SqlDataAdapter comm=new SqlDataAdapter(sql,conn);
5          DataSet isset=new DataSet();
6          comm.Fill(isset,"User");
7          DataView isview=new DataView(isset.Tables["User"]);
8          datagrid1.DataSource=isview;
9          datagrid1.DataBind();

這裡就是使用DataView的方式綁定的,先定義串連,再通過SqlDataAdapter讀取資料,再把資料填充到DataSet中的User表中,把DataSet表中的內容給DataView,然後就綁定DataGrid。其實這很簡單,在我前面發的DataGrid和XML讀取資料中有讀取XML資料繫結的方法,其中還可以設定Sort來排順。

接下來是定義DataGrid中分頁的事件1 datagrid1.CurrentPageIndex=e.NewPageIndex;
2 binding();

其中的binding()就是上面的綁定代碼,這裡可以把之前的綁定代碼寫成函數來調用。
這樣就完成了簡單的資料讀取,綁定以及分頁。 

        在使用的過程中我們有時候需要把查詢的資料進行處理後顯示,比如查詢到使用者的類型1,但是我們需要看到是普通使用者還是特殊使用者,這裡就需要使用自訂控制項來時間了。 1 <%@ Register TagPrefix="user" TagName="money" Src="xxx.ascx" %>

3 <asp:TemplateColumn HeaderText="儲值數量" HeaderStyle-Width="70" ItemStyle-Width="200">
4     <itemtemplate><user:money id="check1" runat="server" uid=<%# DataBinder.Eval(Container.DataItem,"id")%>/> 
5     </itemtemplate>
6 </asp:TemplateColumn>

這裡就是先編寫xxx.ascx,然後在網頁中註冊標籤(第一行就是),然後在DataGrid中使用,這裡要使用asp:TemplateColumn來綁定。其中的<%# DataBinder.Eval(Container.DataItem,"id")%>就是綁定的內容,而前面的就是引用自訂的控制項。順便說一下columns中的Binder 方法:之前示範的是一種,還有的是使用最先的DataFiled的方法,還有一種就是之前示範的用asp:TemplateColumn,不過在itemtemplate中用<%# DataBinder.Eval(Container.DataItem,"id")%>

以上就是一些簡單的DataGrid使用方法,大家交流交流。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.