用了GRIDVIEW一段时间,发现很多人都在问GRIDVIEW的排序功能,有些朋友在我的QQ群(13536330)里面问我,我觉得有三种方法可以实现,但本文我只讲两种,相信可以满足大家的需要了吧。

1、静态实现(直接用GRIDVIEW和SQLDATASOURCE)

HTML代码如下:

 1
 2    <form id="form1" runat="server">
 3    <div>
 4        <asp:GridView ID="CustmGrid" runat="server" AutoGenerateColumns="False"
 5            Width="100%" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" Font-Size="10pt" ForeColor="Black" GridLines="Vertical" PageSize="3" AllowPaging="True" OnPageIndexChanging="CustmGrid_PageIndexChanging" OnRowDataBound="CustmGrid_RowDataBound" AllowSorting="True" DataSourceID="CustmSource">
 6            <Columns>
 7                <asp:HyperLinkField HeaderText="客户名称" DataTextField="CompanyName" SortExpression="CompanyName"  />
 8                <asp:BoundField HeaderText="联系人" DataField="CustmName" />
 9                <asp:BoundField HeaderText="省份" DataField="ProvinceName" />
10                <asp:BoundField HeaderText="城市" DataField="CityName" />
11                <asp:BoundField HeaderText="详细地址" DataField="ZipNum" />
12                <asp:BoundField HeaderText="电话" DataField="TelNum" />
13                <asp:BoundField HeaderText="传真" DataField="FaxNum" />
14                <asp:BoundField HeaderText="手机" DataField="MobileNum" />
15                <asp:BoundField HeaderText="客户类型" DataField="CustmClass" />
16                <asp:BoundField HeaderText="客户来源" DataField="CustmType" />
17                <asp:BoundField HeaderText="客户状态" DataField="CustmStatus" />
18            </Columns>
19            <FooterStyle BackColor="#CCCCCC" />
20            <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
21            <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
22            <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" Height="10px" />
23            <AlternatingRowStyle BackColor="#CCCCCC" />
24        </asp:GridView>
25
26
27        <asp:SqlDataSource ID="CustmSource" runat="server" SelectCommand="select * from CustmInfo" ConnectionString="Server=localhost;DataBase=LixyCRM;User Id=sa;Password=;"></asp:SqlDataSource>
28        
29  <OBJECT id=WebBrowser classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 width=0></OBJECT> 
30  <input type=button value=打印 onclick=document.all.WebBrowser.ExecWB(6,1)>   
31  <input type=button value=直接打印 onclick=document.all.WebBrowser.ExecWB(6,6)>   
32  <input type=button value=页面设置 onclick=document.all.WebBrowser.ExecWB(8,1)>   
33  <input type=button value="打印OK" onclick="return myprint(this);">                         
34  <input type=button value=打印预览 onclick=document.all.WebBrowser.ExecWB(7,1)> 
35        <br />
36        <br />
37</div>
38    </form>

用这种方法,只要在HTML页中写代码就可以了。需要注意的是:
GRIDVIEW中的AllowSorting="True" 和DataSourceID="CustmSource",这两个要指定;
SQLDATASOURCE控件中的SELECTCOMMAND和CONNECTIONSTRING都要指定;

2、动态:

在HTML页面(即.aspx页面)

 1    <form id="form1" runat="server">
 2    <div>
 3        <asp:GridView ID="CustmGrid" runat="server" AutoGenerateColumns="False"
 4            Width="100%" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" Font-Size="10pt" ForeColor="Black" GridLines="Vertical" PageSize="3" AllowPaging="True" OnPageIndexChanging="CustmGrid_PageIndexChanging" OnRowDataBound="CustmGrid_RowDataBound" AllowSorting="True" OnSorting="CustmGrid_Sorting" >
 5            <Columns>
 6                <asp:HyperLinkField HeaderText="客户名称" DataTextField="CompanyName" SortExpression="CompanyName"  />
 7                <asp:BoundField HeaderText="联系人" DataField="CustmName" />
 8                <asp:BoundField HeaderText="省份" DataField="ProvinceName" />
 9                <asp:BoundField HeaderText="城市" DataField="CityName" />
10                <asp:BoundField HeaderText="详细地址" DataField="ZipNum" />
11                <asp:BoundField HeaderText="电话" DataField="TelNum" />
12                <asp:BoundField HeaderText="传真" DataField="FaxNum" />
13                <asp:BoundField HeaderText="手机" DataField="MobileNum" />
14                <asp:BoundField HeaderText="客户类型" DataField="CustmClass" />
15                <asp:BoundField HeaderText="客户来源" DataField="CustmType" />
16                <asp:BoundField HeaderText="客户状态" DataField="CustmStatus" />
17            </Columns>
18            <FooterStyle BackColor="#CCCCCC" />
19            <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
20            <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
21            <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" Height="10px" />
22            <AlternatingRowStyle BackColor="#CCCCCC" />
23        </asp:GridView>
24
25
26        <asp:SqlDataSource ID="CustmSource" runat="server" ></asp:SqlDataSource>
27        
28  <OBJECT id=WebBrowser classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 width=0></OBJECT> 
29  <input type=button value=打印 onclick=document.all.WebBrowser.ExecWB(6,1)>   
30  <input type=button value=直接打印 onclick=document.all.WebBrowser.ExecWB(6,6)>   
31  <input type=button value=页面设置 onclick=document.all.WebBrowser.ExecWB(8,1)>   
32  <input type=button value="打印OK" onclick="return myprint(this);">                         
33  <input type=button value=打印预览 onclick=document.all.WebBrowser.ExecWB(7,1)> 
34        <br />
35        <br />
36</div>
37    </form>

然后在代码页面(即.aspx.cs页面)

 1    public void LoadGrid()
 2    {
 3
 4        this.CustmSource.ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["SqlConnectionStr"];
 5        this.CustmSource.SelectCommand = "select * from CustmInfo";
 6        this.CustmGrid.DataSourceID = "CustmSource";
 7        this.CustmGrid.DataBind();
 8
 9
10
11    }

怎么样,简单吧。(仅供初学者参考)

原文连接:http://blog.csdn.net/lixyvip/archive/2006/09/02/1157439.aspx

转载于:https://www.cnblogs.com/min10/archive/2008/11/13/1332760.html

GRIDVIEW排序 动态实现和静态实现相关推荐

  1. asp.net中GridView排序的手动实现

    GridView自带了数据排序功能.在设计视图下,只能对GridView的排序数据列和排序方向进行静态设置.如果要自己实现排序功能,在后台程序中,则需要用Attributes方式对GridView的这 ...

  2. 红橙Darren视频笔记 代理模式 动态代理和静态代理

    红橙Darren视频笔记 代理模式 动态代理和静态代理(Android API 25) 关于代理模式我之前有过相关的介绍: https://blog.csdn.net/u011109881/artic ...

  3. ElasticSearch 动态映射和静态映射,以及四种字段类型

    文章目录 1.ElasticSearch 映射 1.1 映射分类 1.2 类型推断 2.ElasticSearch 字段类型 2.1 核心类型 2.1.1 字符串类型 2.1.2 数字类型 2.1.3 ...

  4. Android 动态广播与静态广播

    学而时习,稳固而之心 目前推荐使用动态广播,现在已经很就没有使用过静态广播了. 动态广播与静态广播的联系 1 动态广播的速度要快于静态广播 2 静态广播的生存期可以比动态广播的长很多 在Manifes ...

  5. Linux动态库和静态库比较

     Linux动态库和静态库比较文件预览 文件目录树如下,如你所见,非常简单.    1. libtest/    2. |-- lt.c    3. |-- lt.h    4. `-- test.c ...

  6. ios 开发中 动态库 与静态库的区别

    使用静态库的好处 1,模块化,分工合作 2,避免少量改动经常导致大量的重复编译连接 3,也可以重用,注意不是共享使用 动态库使用有如下好处: 1使用动态库,可以将最终可执行文件体积缩小 2使用动态库, ...

  7. 动态参数与静态参数的判断、修改

    视图 v$parameter 中 ISSYS_MODIFIABLE 列能够查看参数属性,ISSYS_MODIFIABLE ='IMMEDIATE'为动态参数. ISSYS_MODIFIABLE ='F ...

  8. Linux中的动态库和静态库(.a/.la/.so/.o)

    为什么80%的码农都做不了架构师?>>>    Linux中的动态库和静态库(.a/.la/.so/.o) Linux中的动态库和静态库(.a/.la/.so/.o) C/C++程序 ...

  9. linux 程序、动态库、静态库内部添加版本号和编译时间

    2019独角兽企业重金招聘Python工程师标准>>> 给程序和库添加版本号和库,有利于维护和升级. 当然你可以在文件名上体现,比如有个程序叫 yun,文件名写为 yun_1.0.2 ...

最新文章

  1. 程序员如何乘风破浪?从数据库历史看技术人发展 | CSDN 高校俱乐部
  2. 在 .NET 框架程序中通过DllImport使用 Win32 API
  3. c winform 上传文件到mysql_Winform下如何上传图片并显示出来。同时保存到数据库...
  4. HTML的dl、dt、dd标记制作表格对决Table制作表
  5. 数学建模大赛赛题解析:Mathorcup高校数学建模挑战赛-基于收得率预测模型的转炉炼钢的成本优化
  6. linux ftp 配额 quota,linux – vsftpd中的配额?
  7. Angular 如何根据一个 class 的定义和数据,动态创建一个该类的实例
  8. Live Rate creation中的Territory check逻辑
  9. 2021高考成绩查询内蒙时间,2021内蒙古高考成绩什么时候几点可以查
  10. java注解中可使用对象_Java注解(二):实战 - 直接使用对象列表生成报表...
  11. k8s安装sqlite3_kubernetes环境部署单节点redis数据库的方法
  12. python读取文件名有中文_[请教]python的中文文件名处理
  13. java中set和get用法_java中 set 和 get
  14. 当天剩余时间,当月剩余时间(秒数),用于redis设置过期时间
  15. 怎么使用计算机自带的摄像头拍视频,电脑怎么用摄像头录制视频
  16. 深入理解 MySQL 主键和唯一(unique)索引
  17. Java基础系列(五)——Collection集合Map源码详解
  18. 标题python自动化测试培训-UnitTest/PyUnit的用法介绍
  19. MAC下配置openCV
  20. AWS免费套餐服务器部署NETCORE网站

热门文章

  1. android版 点击下载,自动点击器最新版
  2. qweb加html文件,将本地html文件加载到Pyside QwebVi中
  3. linux python json,在Python中使用JSON
  4. 小汤学编程之JAVA基础day07——面向对象(二):封装、继承与多态
  5. Asp.Net Core Web Api图片上传(一)集成MongoDB存储实例教程
  6. django请求生命周期
  7. flask+uwsgi+supervisor+nginx在局域网服务器上部署实践
  8. CentOS 7.4 ifconfig, ip/ss, nmcli, nmtui, 配置文件 修改ip信息用法
  9. Java中的变量数据类型补充
  10. ●BZOJ 1396 识别子串