GridView排序加上下箭头(2)

GridView排序加上下箭头
上面的方法很不爽,自己从新做了一个,在我以前的代码基础之上修改的.这种方法才是我要的真正效果.
特公布代码如下:
前端页面代码没变,CS代码.
ContractedBlock.gif ExpandedBlockStart.gif
  1None.gifusing System;
  2None.gifusing System.Data;
  3None.gifusing System.Configuration;
  4None.gifusing System.Collections;
  5None.gifusing System.Web;
  6None.gifusing System.Web.Security;
  7None.gifusing System.Web.UI;
  8None.gifusing System.Web.UI.WebControls;
  9None.gifusing System.Web.UI.WebControls.WebParts;
 10None.gifusing System.Web.UI.HtmlControls;
 11None.gif
 12None.gif
 13None.gifpublic partial class ComEmployee : System.Web.UI.Page
 14ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 15InBlock.gif    protected void Page_Load(object sender, EventArgs e)
 16ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 17InBlock.gif        if (!IsPostBack)
 18ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 19InBlock.gif            this.txtDate.Text = DateTime.Now.ToString("MM/dd/yyyy");
 20InBlock.gif            this.btnGetEmp.Attributes["onclick"= "changeStatus();";
 21InBlock.gif            GridViewBind();
 22ExpandedSubBlockEnd.gif        }

 23ExpandedSubBlockEnd.gif    }

 24InBlock.gif    private void GridViewBind()
 25ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 26InBlock.gif        //監測當前的排序狀況
 27InBlock.gif        if (this.ViewState["orderByName"== null)
 28ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 29InBlock.gif            ViewState["orderByName"= "g_time";
 30InBlock.gif            ViewState["orderByType"= "asc";
 31ExpandedSubBlockEnd.gif        }

 32InBlock.gif
 33InBlock.gif        //獲取當前ViewState中保存的排序
 34InBlock.gif        string orderByName = this.ViewState["orderByName"].ToString();
 35InBlock.gif        string orderByType = this.ViewState["orderByType"].ToString();
 36InBlock.gif        WebClass.NewEmpClass tmp = new WebClass.NewEmpClass();
 37InBlock.gif        DataTable dt = tmp.ComEmployyeeList();
 38InBlock.gif        DataView dv = dt.DefaultView;
 39InBlock.gif        dv.Sort = orderByName+" "+ orderByType;
 40InBlock.gif        myGridView.DataSource = dv;
 41InBlock.gif        myGridView.DataBind();
 42InBlock.gif        labRecordCount.Text = "共<font color='red'>" + dt.Rows.Count.ToString() + "</font>筆記錄";
 43InBlock.gif        labPageCount.Text = "頁次<font color='blue'>" + (myGridView.PageIndex + 1).ToString() + "</font>/<font color='red'>" + myGridView.PageCount.ToString() + "</font>頁";
 44InBlock.gif        labPageRecord.Text = "<font color='red'>" + myGridView.PageSize.ToString() + "</font>筆記錄/頁";
 45InBlock.gif
 46InBlock.gif        if (dt.Rows.Count == 0)
 47ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 48InBlock.gif            btnFirst.Visible = false;
 49InBlock.gif            btnPrev.Visible = false;
 50InBlock.gif            btnNext.Visible = false;
 51InBlock.gif            btnLast.Visible = false;
 52InBlock.gif
 53InBlock.gif            labRecordCount.Visible = false;
 54InBlock.gif            labPageCount.Visible = false;
 55InBlock.gif            labPageRecord.Visible = false;
 56ExpandedSubBlockEnd.gif        }

 57InBlock.gif        else if (myGridView.PageCount == 1)
 58ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 59InBlock.gif            btnFirst.Visible = false;
 60InBlock.gif            btnPrev.Visible = false;
 61InBlock.gif            btnNext.Visible = false;
 62InBlock.gif            btnLast.Visible = false;
 63ExpandedSubBlockEnd.gif        }

 64InBlock.gif
 65InBlock.gif        btnFirst.CommandName = "1";
 66InBlock.gif        btnPrev.CommandName = (myGridView.PageIndex == 0 ? "1" : myGridView.PageIndex.ToString());
 67InBlock.gif        btnNext.CommandName = (myGridView.PageCount == 1 ? myGridView.PageCount.ToString() : (myGridView.PageIndex + 2).ToString());
 68InBlock.gif        btnLast.CommandName = myGridView.PageCount.ToString();
 69InBlock.gif        BtnValidate();
 70ExpandedSubBlockEnd.gif    }

 71InBlock.gif    private void BtnValidate()
 72ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 73InBlock.gif        if (myGridView.PageIndex + 1 >= myGridView.PageCount)
 74ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 75InBlock.gif            btnNext.Enabled = false;
 76InBlock.gif            btnLast.Enabled = false;
 77ExpandedSubBlockEnd.gif        }

 78InBlock.gif        else
 79ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 80InBlock.gif            btnNext.Enabled = true;
 81InBlock.gif            btnLast.Enabled = true;
 82ExpandedSubBlockEnd.gif        }

 83InBlock.gif        if (myGridView.PageIndex + 1 <= 1)
 84ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 85InBlock.gif            btnPrev.Enabled = false;
 86InBlock.gif            btnFirst.Enabled = false;
 87ExpandedSubBlockEnd.gif        }

 88InBlock.gif        else
 89ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 90InBlock.gif            btnPrev.Enabled = true;
 91InBlock.gif            btnFirst.Enabled = true;
 92ExpandedSubBlockEnd.gif        }

 93ExpandedSubBlockEnd.gif    }

 94InBlock.gif    protected void PagerButtonClick(object sender, EventArgs e)
 95ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 96InBlock.gif        myGridView.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandName) - 1;
 97InBlock.gif        GridViewBind();
 98ExpandedSubBlockEnd.gif    }

 99InBlock.gif    protected void myGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
100ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
101InBlock.gif        myGridView.PageIndex = e.NewPageIndex;
102InBlock.gif        GridViewBind();
103ExpandedSubBlockEnd.gif    }

104InBlock.gif    protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
105ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
106InBlock.gif        if (e.Row.RowIndex != -1)
107ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
108InBlock.gif            int orderID = this.myGridView.PageIndex * this.myGridView.PageSize + e.Row.RowIndex + 1;
109InBlock.gif            e.Row.Cells[0].Text = orderID.ToString();
110ExpandedSubBlockEnd.gif        }

111ExpandedSubBlockEnd.gif    }

112InBlock.gif    protected void myGridView_Sorting(object sender, GridViewSortEventArgs e)
113ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
114InBlock.gif        this.setViewState(e.SortExpression, this.ViewState["orderByType"].ToString() == "asc" ? "desc" : "asc");
115InBlock.gif        GridViewBind();
116ExpandedSubBlockEnd.gif    }

117ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
118InBlock.gif    /// 以ViewState控制排序,寫入排序字段名及排序方式
119ExpandedSubBlockEnd.gif    /// </summary>

120InBlock.gif    private void setViewState(string orderByName, string orderByType)
121ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
122InBlock.gif        ViewState["orderByName"= orderByName;
123InBlock.gif        ViewState["orderByType"= orderByType;
124ExpandedSubBlockEnd.gif    }

125InBlock.gif
126InBlock.gif    private string SortExpression
127ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
128InBlock.gif        get
129ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
130InBlock.gif            if (ViewState["orderByName"!= null && ViewState["orderByName"].ToString() != string.Empty)
131ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
132InBlock.gif                return ViewState["orderByName"].ToString();
133ExpandedSubBlockEnd.gif            }

134InBlock.gif            else
135ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
136InBlock.gif                return "";
137ExpandedSubBlockEnd.gif            }

138ExpandedSubBlockEnd.gif        }

139InBlock.gif        set
140ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
141InBlock.gif            ViewState["orderByName"= value;
142ExpandedSubBlockEnd.gif        }

143ExpandedSubBlockEnd.gif    }

144InBlock.gif    private string sort
145ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
146InBlock.gif        get
147ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
148InBlock.gif            if (ViewState["orderByType"!= null && ViewState["orderByType"].ToString() != string.Empty)
149ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
150InBlock.gif                return ViewState["orderByType"].ToString();
151ExpandedSubBlockEnd.gif            }

152InBlock.gif            else
153ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
154InBlock.gif                return "";
155ExpandedSubBlockEnd.gif            }

156ExpandedSubBlockEnd.gif        }

157InBlock.gif        set
158ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
159InBlock.gif            ViewState["orderByType"= value;
160ExpandedSubBlockEnd.gif        }

161ExpandedSubBlockEnd.gif    }

162InBlock.gif    protected void imgbtnUpdate_Click(object sender, ImageClickEventArgs e)
163ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
164InBlock.gif        GridViewBind();
165ExpandedSubBlockEnd.gif    }

166InBlock.gif    protected void btnGetEmp_Click(object sender, EventArgs e)
167ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
168InBlock.gif        string strEmpno = Request.Form.Get("chkID");
169InBlock.gif        WebClass.NewEmpClass tmp = new WebClass.NewEmpClass();
170InBlock.gif        tmp.CheckNewEmployyee(strEmpno);
171ExpandedSubBlockEnd.gif    }

172InBlock.gif    protected void myGridView_RowCreated(object sender, GridViewRowEventArgs e)
173ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
174InBlock.gif        for (int i = 0; i < myGridView.Columns.Count; i++)
175ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
176InBlock.gif            //if (i == 0) continue;
177InBlock.gif            if (e.Row.RowIndex == -1)
178ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
179InBlock.gif                if (myGridView.Columns[i].SortExpression == SortExpression)
180ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
181InBlock.gif                    try
182ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
183InBlock.gif                        TableCell tableCell = e.Row.Cells[i];
184InBlock.gif                        Label lblSorted = new Label();
185InBlock.gif                        lblSorted.Font.Name = "webdings";
186InBlock.gif                        lblSorted.Width = 20;
187InBlock.gif                        lblSorted.Text = (sort == "asc" ? "6" : "5");
188InBlock.gif                        tableCell.Controls.Add(lblSorted);
189ExpandedSubBlockEnd.gif                    }

190ExpandedSubBlockStart.gifContractedSubBlock.gif                    catch dot.gif{ }
191ExpandedSubBlockEnd.gif                }

192ExpandedSubBlockEnd.gif            }

193ExpandedSubBlockEnd.gif        }

194InBlock.gif
195ExpandedSubBlockEnd.gif    }

196ExpandedBlockEnd.gif}

197None.gif

在实现这种方法时有一些感触,分享如下:
1.GridGriew中的DataView.sort属性排序时可以保存状态,DataGrid就不行,只能对当前页进行排序,所以每次实现这种功能时都把参数传到数据层.很郁闷.
2.忘記了,剛才還記得,哎記憶力不好了,想起來了再寫,要幹活了。
3.orderByName和orderByType之間要加空格
eg:        DataView dv = dt.DefaultView;
        dv.Sort = orderByName+" "+ orderByType;
4.向上向下的箭頭也可以換成圖片,webdings字體應該速度更快。
效果如下:
jt.jpg

转载于:https://www.cnblogs.com/cnaspnet/archive/2006/08/25/486338.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值