打印本文 打印本文 关闭窗口 关闭窗口
让Asp.NET的DataGrid可排序、可选择、可分页
作者:武汉SEO闵涛  文章来源:敏韬网  点击数1516  更新时间:2009/4/23 10:49:18  文章录入:mintao  责任编辑:mintao

  

让Asp.NET的DataGrid可排序、可选择、可分页

       DataGrid是Asp.NET中的一个重要的控件,经常我们都将DataGrid做成可分页的和可排序的,有时还需要加上选择功能。这些都是经常需要用到的方法,其实是比较简单的。

 

设计思路:

为了方便起见,我们连接SQL Server 2000的NorthWind数据库的Orders表,从数据库里得到此表的数据视图。利用DataGrid的SortCommand事件实现排序用一个模板列加上CheckBox控件实现选择。可用DataGrid的属性生成器的“分页”选项或者自己修改HTML实现分页。

 

HTML:

  • 添加一个DataGrid,命名为dgOrder。
  • 添加了一个模板列,模板列里放一个名为Cb的CheckBox控件。此列用来实现选择
  • 为要排序的每个列加上排序表达式SortExpression。
  • 利用列的DataFormatString来格式化列,象DataFormatString="{0:d}"显示日期格式。
  • 设置PageSize="15"每页显示15行数据,AllowPaging="True" 为允许分页 。

 

整个HTML页代码:

<form id="Form1" method="post" runat="server">

       <asp:datagrid id="dgOrder" runat="server" Height="515px" Width="718px" AutoGenerateColumns="False" AllowSorting="True" CellPadding="4" BorderWidth="1px" BorderColor="#A0ABEB" PageSize="15" BorderStyle="Solid" BackColor="White" GridLines="Vertical" ForeColor="Black" AllowPaging="True" ShowFooter="True">

              <SelectedItemStyle ForeColor="White" BackColor="Black"></SelectedItemStyle>

              <AlternatingItemStyle BackColor="#EEEEEE"></AlternatingItemStyle>

              <HeaderStyle HorizontalAlign="Center" ForeColor="White" BorderColor="#6876C5" BackColor="#6876C5"></HeaderStyle>

              <FooterStyle ForeColor="White" BackColor="#6876C5"></FooterStyle>

              <Columns>

                     <asp:TemplateColumn>

                            <ItemTemplate>

                                   <FONT face="宋体">

                                          <asp:CheckBox id="Cb" runat="server"></asp:CheckBox></FONT>

                            </ItemTemplate>

                     </asp:TemplateColumn>

                     <asp:BoundColumn DataField="orderid" SortExpression="orderid" HeaderText="ID">

                            <HeaderStyle Width="180px"></HeaderStyle>

                     </asp:BoundColumn>

                     <asp:BoundColumn DataField="ShipCountry" SortExpression="ShipCountry" HeaderText="ShipCountry">

                            <HeaderStyle Width="180px"></HeaderStyle>

                     </asp:BoundColumn>

                     <asp:BoundColumn DataField="ShippedDate" SortExpression="ShippedDate" HeaderText="ShippedDate" DataFormatString="{0:d}">

                            <HeaderStyle Width="180px"></HeaderStyle>

                     </asp:BoundColumn>

                     <asp:BoundColumn DataField="Freight" SortExpression="Freight" HeaderText="Freight">

                            <HeaderStyle Width="180px"></HeaderStyle>

                     </asp:BoundColumn>

                     <asp:BoundColumn DataField="ShipAddress" SortExpression="ShipAddress" HeaderText="ShipAddress">

                            <HeaderStyle Width="480px"></HeaderStyle>

[1] [2] [3] [4]  下一页

打印本文 打印本文 关闭窗口 关闭窗口