GridView排序加上下箭头

ASPX:
None.gif            < table  width ="778"  border ="0"  align ="center"  cellpadding ="0"  cellspacing ="0" >
None.gif            
< tr >
None.gif               
< td  align ="center" >
None.gif                    
< asp:GridView   ID ="myGridView"  runat ="server"  AllowPaging ="True"  DataKeyNames ="id"
None.gif                        AutoGenerateColumns
="False"  Width ="100%"  PageSize ="25"  OnRowDataBound ="myGridView_RowDataBound"  AllowSorting ="True"  OnSorting ="myGridView_Sorting"  OnPageIndexChanging ="myGridView_PageIndexChanging"  EmptyDataText ="沒有記錄"  OnRowCreated ="myGridView_RowCreated" >
None.gif                        
< Columns >
None.gif                            
< asp:BoundField  HeaderText ="序號"   />
None.gif                            
< asp:BoundField  DataField ="empno"  SortExpression ="empno"  HeaderText ="工號"   />
None.gif                            
< asp:BoundField  DataField ="cname"  SortExpression ="cname"  HeaderText ="姓名"   />
None.gif                            
< asp:BoundField  DataField ="sex"  SortExpression ="sex"  HeaderText ="性別"   />
None.gif                            
< asp:BoundField  DataField ="age"  SortExpression ="age"  HeaderText ="年齡"   />
None.gif                            
< asp:BoundField  DataField ="linecode"  SortExpression ="linecode"  HeaderText ="拉名"    />
None.gif                            
< asp:BoundField  DataField ="hold_date"  DataFormatString ="{0:MM月dd日yyyy年}"   HeaderText ="入廠日期"   HtmlEncode ="False"  SortExpression ="hold_date" />
None.gif                            
< asp:BoundField  DataField ="g_time"  DataFormatString ="{0:MM月dd日yyyy年}"   HeaderText ="體檢日期"   HtmlEncode ="False"  SortExpression ="g_time" />
None.gif                            
< asp:CommandField  HeaderText ="操作"  ShowEditButton ="True"   />
None.gif                            
< asp:TemplateField >
None.gif                                
< HeaderTemplate >
None.gif                                    
< input  id ="CheckAll"  type ="checkbox"  onclick ="selectAll(this)"   />
None.gif                                
</ HeaderTemplate >
None.gif                                
< ItemTemplate >
None.gif                                    
< input  id ="chkID"  name ="chkID"  type ="checkbox"  value ='<%#  Eval("empno")% > ' οnclick="selectOne(this)" />
None.gif                                
</ ItemTemplate >
None.gif                            
</ asp:TemplateField >
None.gif                        
</ Columns >
None.gif                        
< HeaderStyle  BackColor ="Gainsboro"   />
None.gif                        
< PagerSettings  PageButtonCount ="15"  Visible ="False"   />
None.gif                    
</ asp:GridView  >
None.gif              
</ td >
None.gif            
</ tr >
None.gif        
</ table >
None.gif       
< table  width ="778"  height ="25"  border ="0"  align ="center"  cellpadding ="0"  cellspacing ="0" >
None.gif        
< tr >
None.gif          
< td >
None.gif            
< asp:Label  ID ="labRecordCount"  runat ="server" ></ asp:Label > &nbsp;
None.gif            
< asp:Label  ID ="labPageCount"  runat ="server" ></ asp:Label > &nbsp;
None.gif            
< asp:Label  ID ="labPageRecord"  runat ="server" ></ asp:Label > &nbsp;
None.gif            
</ td >
None.gif          
< td  align ="right" >
None.gif          
< asp:LinkButton  ID ="btnFirst"  CommandArgument ="first"  OnClick ="PagerButtonClick"  runat ="server" > 首 页 </ asp:LinkButton > &nbsp;
None.gif            
< asp:LinkButton  ID ="btnPrev"  CommandArgument ="prev"  OnClick ="PagerButtonClick"  runat ="server" > 上一页 </ asp:LinkButton > &nbsp;
None.gif            
< asp:LinkButton  ID ="btnNext"  CommandArgument ="next"  OnClick ="PagerButtonClick"  runat ="server" > 下一页 </ asp:LinkButton > &nbsp;
None.gif          
< asp:LinkButton  ID ="btnLast"  CommandArgument ="last"  OnClick ="PagerButtonClick"  runat ="server" > 尾 页 </ asp:LinkButton >
None.gif          
</ td >
None.gif        
</ tr >
None.gif      
</ table >

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        WebClass.NewEmpClass tmp = new WebClass.NewEmpClass();
 27InBlock.gif        DataTable dt = tmp.ComEmployyeeList();
 28InBlock.gif        DataView dv = dt.DefaultView;
 29InBlock.gif        if (ViewState["SortExpression"!= null)
 30ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 31InBlock.gif            dv.Sort = ViewState["SortExpression"].ToString() + " " + ViewState["SortDirection"].ToString();
 32ExpandedSubBlockEnd.gif        }

 33InBlock.gif        myGridView.DataSource = dv;
 34InBlock.gif        myGridView.DataBind();
 35InBlock.gif        labRecordCount.Text = "共<font color='red'>" + dt.Rows.Count.ToString() + "</font>筆記錄";
 36InBlock.gif        labPageCount.Text = "頁次<font color='blue'>" + (myGridView.PageIndex + 1).ToString() + "</font>/<font color='red'>" + myGridView.PageCount.ToString() + "</font>頁";
 37InBlock.gif        labPageRecord.Text = "<font color='red'>" + myGridView.PageSize.ToString() + "</font>筆記錄/頁";
 38InBlock.gif
 39InBlock.gif        if (dt.Rows.Count == 0)
 40ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 41InBlock.gif            btnFirst.Visible = false;
 42InBlock.gif            btnPrev.Visible = false;
 43InBlock.gif            btnNext.Visible = false;
 44InBlock.gif            btnLast.Visible = false;
 45InBlock.gif
 46InBlock.gif            labRecordCount.Visible = false;
 47InBlock.gif            labPageCount.Visible = false;
 48InBlock.gif            labPageRecord.Visible = false;
 49ExpandedSubBlockEnd.gif        }

 50InBlock.gif        else if (myGridView.PageCount == 1)
 51ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 52InBlock.gif            btnFirst.Visible = false;
 53InBlock.gif            btnPrev.Visible = false;
 54InBlock.gif            btnNext.Visible = false;
 55InBlock.gif            btnLast.Visible = false;
 56ExpandedSubBlockEnd.gif        }

 57InBlock.gif
 58InBlock.gif        btnFirst.CommandName = "1";
 59InBlock.gif        btnPrev.CommandName = (myGridView.PageIndex == 0 ? "1" : myGridView.PageIndex.ToString());
 60InBlock.gif        btnNext.CommandName = (myGridView.PageCount == 1 ? myGridView.PageCount.ToString() : (myGridView.PageIndex + 2).ToString());
 61InBlock.gif        btnLast.CommandName = myGridView.PageCount.ToString();
 62InBlock.gif        BtnValidate();
 63ExpandedSubBlockEnd.gif    }

 64InBlock.gif    private void BtnValidate()
 65ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 66InBlock.gif        if (myGridView.PageIndex + 1 >= myGridView.PageCount)
 67ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 68InBlock.gif            btnNext.Enabled = false;
 69InBlock.gif            btnLast.Enabled = false;
 70ExpandedSubBlockEnd.gif        }

 71InBlock.gif        else
 72ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 73InBlock.gif            btnNext.Enabled = true;
 74InBlock.gif            btnLast.Enabled = true;
 75ExpandedSubBlockEnd.gif        }

 76InBlock.gif        if (myGridView.PageIndex + 1 <= 1)
 77ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 78InBlock.gif            btnPrev.Enabled = false;
 79InBlock.gif            btnFirst.Enabled = false;
 80ExpandedSubBlockEnd.gif        }

 81InBlock.gif        else
 82ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 83InBlock.gif            btnPrev.Enabled = true;
 84InBlock.gif            btnFirst.Enabled = true;
 85ExpandedSubBlockEnd.gif        }

 86ExpandedSubBlockEnd.gif    }

 87InBlock.gif    protected void PagerButtonClick(object sender, EventArgs e)
 88ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 89InBlock.gif        myGridView.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandName) - 1;
 90InBlock.gif        GridViewBind();
 91ExpandedSubBlockEnd.gif    }

 92InBlock.gif    protected void myGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
 93ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 94InBlock.gif        myGridView.PageIndex = e.NewPageIndex;
 95InBlock.gif        GridViewBind();
 96ExpandedSubBlockEnd.gif    }

 97InBlock.gif    protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
 98ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 99InBlock.gif        if (e.Row.RowIndex != -1)
100ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
101InBlock.gif            int orderID = this.myGridView.PageIndex * this.myGridView.PageSize + e.Row.RowIndex + 1;
102InBlock.gif            e.Row.Cells[0].Text = orderID.ToString();
103ExpandedSubBlockEnd.gif        }

104ExpandedSubBlockEnd.gif    }

105InBlock.gif    protected void myGridView_Sorting(object sender, GridViewSortEventArgs e)
106ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
107InBlock.gif        ViewState["SortExpression"= e.SortExpression;
108InBlock.gif        if (ViewState["SortDirection"== null)
109ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
110InBlock.gif            ViewState["SortDirection"= "asc";
111ExpandedSubBlockEnd.gif        }

112InBlock.gif        else
113ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
114InBlock.gif            if (ViewState["SortDirection"].ToString() == "asc")
115ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
116InBlock.gif                ViewState["SortDirection"= "desc";
117ExpandedSubBlockEnd.gif            }

118InBlock.gif            else
119ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
120InBlock.gif                ViewState["SortDirection"= "asc";
121ExpandedSubBlockEnd.gif            }

122ExpandedSubBlockEnd.gif        }

123InBlock.gif        GridViewBind();
124ExpandedSubBlockEnd.gif    }

125InBlock.gif    protected void imgbtnUpdate_Click(object sender, ImageClickEventArgs e)
126ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
127InBlock.gif        GridViewBind();
128ExpandedSubBlockEnd.gif    }

129InBlock.gif    protected void btnGetEmp_Click(object sender, EventArgs e)
130ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
131InBlock.gif        string strEmpno = Request.Form.Get("chkID");
132InBlock.gif        WebClass.NewEmpClass tmp = new WebClass.NewEmpClass();
133InBlock.gif        tmp.CheckNewEmployyee(strEmpno);
134ExpandedSubBlockEnd.gif    }

135InBlock.gif    protected void myGridView_RowCreated(object sender, GridViewRowEventArgs e)
136ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
137InBlock.gif        if (e.Row != null && e.Row.RowType == DataControlRowType.Header)
138ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
139InBlock.gif            string strSortBy = (string)ViewState["SortExpression"];
140InBlock.gif            string strSortAscending = (string)ViewState["SortAscending"];
141InBlock.gif            string strOrder = (strSortAscending == "yes" ? "5" : "6");
142InBlock.gif
143InBlock.gif            for (int i = 0; i < myGridView.Columns.Count; i++)
144ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
145InBlock.gif                if (strSortBy == myGridView.Columns[i].SortExpression)
146ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
147InBlock.gif                    TableCell cell = e.Row.Cells[i];
148InBlock.gif                    Label lblSorted = new Label();
149InBlock.gif                    lblSorted.Font.Name = "webdings";
150InBlock.gif                    lblSorted.Font.Size = FontUnit.XSmall;
151InBlock.gif                    lblSorted.Text = strOrder;
152InBlock.gif                    cell.Controls.Add(lblSorted);
153ExpandedSubBlockEnd.gif                }

154ExpandedSubBlockEnd.gif            }

155ExpandedSubBlockEnd.gif        }

156ExpandedSubBlockEnd.gif    }

157ExpandedBlockEnd.gif}

158None.gif

有点郁闷,在DataGrid中,页面加载的时候默认排序字段会有箭头.
GridView同样的原理就没有.只有点击的时候才有,这样的话在页面加载时就不知道按照什么排序.

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值