用了GRIDVIEW一段时间,发现很多人都在问GRIDVIEW的排序功能,有些朋友在我的QQ群(13536330)里面问我,我觉得有三种方法可以实现,但本文我只讲两种,相信可以满足大家的需要了吧。
1、静态实现(直接用GRIDVIEW和SQLDATASOURCE)
HTML代码如下:
用这种方法,只要在HTML页中写代码就可以了。需要注意的是:
GRIDVIEW中的AllowSorting="True" 和DataSourceID="CustmSource",这两个要指定;
SQLDATASOURCE控件中的SELECTCOMMAND和CONNECTIONSTRING都要指定;
2、动态:
在HTML页面(即.aspx页面)
然后在代码页面(即.aspx.cs页面)
怎么样,简单吧。(仅供初学者参考)
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>
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>
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 }
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