GridView排序加上下箭头上面的方法很不爽,自己从新做了一个,在我以前的代码基础之上修改的.这种方法才是我要的真正效果.
特公布代码如下:
前端页面代码没变,CS代码.
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Collections;
5
using System.Web;
6
using System.Web.Security;
7
using System.Web.UI;
8
using System.Web.UI.WebControls;
9
using System.Web.UI.WebControls.WebParts;
10
using System.Web.UI.HtmlControls;
11![](/Images/OutliningIndicators/None.gif)
12![](/Images/OutliningIndicators/None.gif)
13
public partial class ComEmployee : System.Web.UI.Page
14![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
15
protected void Page_Load(object sender, EventArgs e)
16![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
17
if (!IsPostBack)
18![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
19
this.txtDate.Text = DateTime.Now.ToString("MM/dd/yyyy");
20
this.btnGetEmp.Attributes["onclick"] = "changeStatus();";
21
GridViewBind();
22
}
23
}
24
private void GridViewBind()
25![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
26
//監測當前的排序狀況
27
if (this.ViewState["orderByName"] == null)
28![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
29
ViewState["orderByName"] = "g_time";
30
ViewState["orderByType"] = "asc";
31
}
32![](/Images/OutliningIndicators/InBlock.gif)
33
//獲取當前ViewState中保存的排序
34
string orderByName = this.ViewState["orderByName"].ToString();
35
string orderByType = this.ViewState["orderByType"].ToString();
36
WebClass.NewEmpClass tmp = new WebClass.NewEmpClass();
37
DataTable dt = tmp.ComEmployyeeList();
38
DataView dv = dt.DefaultView;
39
dv.Sort = orderByName+" "+ orderByType;
40
myGridView.DataSource = dv;
41
myGridView.DataBind();
42
labRecordCount.Text = "共<font color='red'>" + dt.Rows.Count.ToString() + "</font>筆記錄";
43
labPageCount.Text = "頁次<font color='blue'>" + (myGridView.PageIndex + 1).ToString() + "</font>/<font color='red'>" + myGridView.PageCount.ToString() + "</font>頁";
44
labPageRecord.Text = "<font color='red'>" + myGridView.PageSize.ToString() + "</font>筆記錄/頁";
45![](/Images/OutliningIndicators/InBlock.gif)
46
if (dt.Rows.Count == 0)
47![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
48
btnFirst.Visible = false;
49
btnPrev.Visible = false;
50
btnNext.Visible = false;
51
btnLast.Visible = false;
52![](/Images/OutliningIndicators/InBlock.gif)
53
labRecordCount.Visible = false;
54
labPageCount.Visible = false;
55
labPageRecord.Visible = false;
56
}
57
else if (myGridView.PageCount == 1)
58![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
59
btnFirst.Visible = false;
60
btnPrev.Visible = false;
61
btnNext.Visible = false;
62
btnLast.Visible = false;
63
}
64![](/Images/OutliningIndicators/InBlock.gif)
65
btnFirst.CommandName = "1";
66
btnPrev.CommandName = (myGridView.PageIndex == 0 ? "1" : myGridView.PageIndex.ToString());
67
btnNext.CommandName = (myGridView.PageCount == 1 ? myGridView.PageCount.ToString() : (myGridView.PageIndex + 2).ToString());
68
btnLast.CommandName = myGridView.PageCount.ToString();
69
BtnValidate();
70
}
71
private void BtnValidate()
72![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
73
if (myGridView.PageIndex + 1 >= myGridView.PageCount)
74![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
75
btnNext.Enabled = false;
76
btnLast.Enabled = false;
77
}
78
else
79![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
80
btnNext.Enabled = true;
81
btnLast.Enabled = true;
82
}
83
if (myGridView.PageIndex + 1 <= 1)
84![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
85
btnPrev.Enabled = false;
86
btnFirst.Enabled = false;
87
}
88
else
89![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
90
btnPrev.Enabled = true;
91
btnFirst.Enabled = true;
92
}
93
}
94
protected void PagerButtonClick(object sender, EventArgs e)
95![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
96
myGridView.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandName) - 1;
97
GridViewBind();
98
}
99
protected void myGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
100![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
101
myGridView.PageIndex = e.NewPageIndex;
102
GridViewBind();
103
}
104
protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
105![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
106
if (e.Row.RowIndex != -1)
107![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
108
int orderID = this.myGridView.PageIndex * this.myGridView.PageSize + e.Row.RowIndex + 1;
109
e.Row.Cells[0].Text = orderID.ToString();
110
}
111
}
112
protected void myGridView_Sorting(object sender, GridViewSortEventArgs e)
113![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
114
this.setViewState(e.SortExpression, this.ViewState["orderByType"].ToString() == "asc" ? "desc" : "asc");
115
GridViewBind();
116
}
117![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
118
/// 以ViewState控制排序,寫入排序字段名及排序方式
119
/// </summary>
120
private void setViewState(string orderByName, string orderByType)
121![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
122
ViewState["orderByName"] = orderByName;
123
ViewState["orderByType"] = orderByType;
124
}
125![](/Images/OutliningIndicators/InBlock.gif)
126
private string SortExpression
127![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
128
get
129![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
130
if (ViewState["orderByName"] != null && ViewState["orderByName"].ToString() != string.Empty)
131![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
132
return ViewState["orderByName"].ToString();
133
}
134
else
135![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
136
return "";
137
}
138
}
139
set
140![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
141
ViewState["orderByName"] = value;
142
}
143
}
144
private string sort
145![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
146
get
147![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
148
if (ViewState["orderByType"] != null && ViewState["orderByType"].ToString() != string.Empty)
149![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
150
return ViewState["orderByType"].ToString();
151
}
152
else
153![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
154
return "";
155
}
156
}
157
set
158![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
159
ViewState["orderByType"] = value;
160
}
161
}
162
protected void imgbtnUpdate_Click(object sender, ImageClickEventArgs e)
163![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
164
GridViewBind();
165
}
166
protected void btnGetEmp_Click(object sender, EventArgs e)
167![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
168
string strEmpno = Request.Form.Get("chkID");
169
WebClass.NewEmpClass tmp = new WebClass.NewEmpClass();
170
tmp.CheckNewEmployyee(strEmpno);
171
}
172
protected void myGridView_RowCreated(object sender, GridViewRowEventArgs e)
173![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
174
for (int i = 0; i < myGridView.Columns.Count; i++)
175![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
176
//if (i == 0) continue;
177
if (e.Row.RowIndex == -1)
178![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
179
if (myGridView.Columns[i].SortExpression == SortExpression)
180![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
181
try
182![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
183
TableCell tableCell = e.Row.Cells[i];
184
Label lblSorted = new Label();
185
lblSorted.Font.Name = "webdings";
186
lblSorted.Width = 20;
187
lblSorted.Text = (sort == "asc" ? "6" : "5");
188
tableCell.Controls.Add(lblSorted);
189
}
190![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
catch
{ }
191
}
192
}
193
}
194![](/Images/OutliningIndicators/InBlock.gif)
195
}
196
}
197![](/Images/OutliningIndicators/None.gif)
在实现这种方法时有一些感触,分享如下:
1.GridGriew中的DataView.sort属性排序时可以保存状态,DataGrid就不行,只能对当前页进行排序,所以每次实现这种功能时都把参数传到数据层.很郁闷.
2.忘記了,剛才還記得,哎記憶力不好了,想起來了再寫,要幹活了。
3.orderByName和orderByType之間要加空格
eg: DataView dv = dt.DefaultView;
dv.Sort = orderByName+" "+ orderByType;
4.向上向下的箭頭也可以換成圖片,webdings字體應該速度更快。
效果如下:
![](/images/cnblogs_com/cnaspnet/jt.jpg)