用了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![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
26![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
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
>
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
2
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
3
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
4
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
5
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
6
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
7
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
8
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
9
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
10
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
11
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
12
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
13
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
14
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
15
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
16
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
17
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
18
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
19
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
20
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
21
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
22
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
23
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
24
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
25
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
26
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
27
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
28
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
29
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
30
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
31
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
32
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
33
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
34
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
35
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
36
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
37
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
38
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
用这种方法,只要在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![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
25![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
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
>
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
2
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
3
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
4
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
5
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
6
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
7
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
8
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
9
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
10
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
11
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
12
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
13
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
14
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
15
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
16
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
17
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
18
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
19
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
20
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
21
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
22
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
23
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
24
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
25
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
26
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
27
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
28
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
29
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
30
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
31
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
32
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
33
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
34
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
35
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
36
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
37
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
然后在代码页面(即.aspx.cs页面)
1
public
void
LoadGrid()
2![](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
{
3![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
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![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
9![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
10![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
11
}
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
2
![](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/0be121fa5b8988fbabbbc526af3b0fc0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
3
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
4
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
5
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
6
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
7
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
8
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
9
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
10
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
11
![](https://i-blog.csdnimg.cn/blog_migrate/4fd96b3cf02f4c7b5c8964ac8167f7af.gif)
怎么样,简单吧。(仅供初学者参考)
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1157439