#region 私有变量

        
protected string JS = "";
        
string SortExpression = "";//排序表达式
        string SortDirection = "";//排序方向

#endregion

#region 事件

/// <summary>
/// 生成排序列旁边的小箭头
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gvData_RowCreated(object sender, GridViewRowEventArgs e)
{
            
if (e.Row.RowType == DataControlRowType.Header)
            {
                
foreach (TableCell tc in e.Row.Cells)
                {
                    
if (tc.Controls.Count > 0)//这里要判断一下此时是不是已经生成了linkbutton
                    {
                        
string s1 = ((LinkButton)tc.Controls[0]).Text;
                        ((LinkButton)tc.Controls[
0]).Text = s1.Replace(s1, s1 + "<font face='Webdings'>5</font>");
                    }
                    
if (tc.Controls.Count > 0 && tc.Controls[0].GetType().ToString() == "System.Web.UI.WebControls.DataControlLinkButton")
                    {

                        
if (((LinkButton)tc.Controls[0]).CommandArgument == SortExpression)
                        {
                            
string s2 = ((LinkButton)tc.Controls[0]).Text;
                            
if (SortDirection == "DESC")
                            {
                                ((LinkButton)tc.Controls[
0]).Text = s2.Replace("5""6");
                            }
                        }
                    }
                }
            }
}

/// <summary>
/// 排序方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gvData_Sorting(object sender, GridViewSortEventArgs e)
{
            
//得到排序条件
            SortExpression = e.SortExpression.ToString();
            
//设置排序为升序
            SortDirection = "ASC";
            
//当当前排序为升序时,修改成降序
            if (this.gridView.Attributes["SortDirection"== "ASC")
            {
                SortDirection 
= "DESC";
            }
            
//设置GridView的排序状态
            this.gridView.Attributes["SortExpression"= SortExpression;
            
this.gridView.Attributes["SortDirection"= SortDirection;
            BindGridView();
}

#endregion


#region 私有方法

/// <summary>
/// 根据排序条件重新绑定GridView
/// </summary>
private void BindGridView()
{
            
string sortExpression = this.gridView.Attributes["SortExpression"];
            
string sortDirection = this.gridView.Attributes["SortDirection"];
            
if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
            {
                DataSource.DefaultView.Sort 
= string.Format("{0} {1}", sortExpression, sortDirection);
            }
            
this.gridView.DataSource = DataSource;
            
this.gridView.DataBind();
}

#endregion

 

 

以上就是全部的代码~