DataGrid排序中加入方向标识核心代碼

在DataGrid中根據排序方向,在HeaderText加入排序方向.代碼如下:
   ///   <summary>
    
///  得到數據,根據分頁Index及排序ID綁定數據
    
///   </summary>
    
///   <param name="iPageIndex"> 頁碼 </param>
    
///   <param name="sSortID"> 排序的ID </param>
     private   void  Copmmon( int  iPageIndex, string  sSortID)
    {        
        
string  sDeptID  =  Request.QueryString[ " strDeptID " ];
        
string  sQuery  =   " select UioID,UioName from Hp_UserInfo where UioDeptID='{0}' " ;
        sQuery 
=   string .Format(sQuery, sDeptID);
        
if  ( ! string .IsNullOrEmpty(sSortID)  &&  sSortID  !=   "" )
        {
            
string  sSortDirection  =   " DESC " // 为排序方向变量赋初值
             if  (sSortDirection  ==  grdRequireList.Attributes[ " GrdSortDirection " ])
            {
                
// 获得下一次的排序状态
                sSortDirection  =  (grdRequireList.Attributes[ " GrdSortDirection " ].ToString()  ==  sSortDirection  ?   " ASC "  :  " DESC " );
            }
            grdRequireList.Attributes[
" GrdSortDirection " =  sSortDirection;
            
string  sOrder  =   "  Order by {0} {1} " ;
            sOrder 
=   string .Format(sOrder, sSortID, sSortDirection);
            sQuery 
=  sQuery  +  sOrder;

            
// 排序图标
             char [] cArray  =   new   char [] {  ' ' ' '  };
            
// 加方向标
             for  ( int  iSor  =   0 ; iSor  <  grdRequireList.Columns.Count; iSor ++ )
            {
                
if  (grdRequireList.Columns[iSor].SortExpression.ToString()  ==  sSortID)
                {
                    
int  iColindex  =  iSor;
                    
if  (sSortDirection == " DESC " )
                    {
                        grdRequireList.Columns[iSor].HeaderText 
=  grdRequireList.Columns[iSor].HeaderText.Trim(cArray);
                        grdRequireList.Columns[iColindex].HeaderText 
=  grdRequireList.Columns[iColindex].HeaderText  +   " " ;
                    }
                    
else
                    {
                        grdRequireList.Columns[iSor].HeaderText 
=  grdRequireList.Columns[iSor].HeaderText.Trim(cArray);
                        grdRequireList.Columns[iColindex].HeaderText 
=  grdRequireList.Columns[iColindex].HeaderText  +   " " ;
                    }
                   
                }
                
else
                {
                    grdRequireList.Columns[iSor].HeaderText 
=  grdRequireList.Columns[iSor].HeaderText.Trim(cArray);
                }
            }
        }       

        DataView dvApplyers 
=  TDAIClient.BusinessDB.GetDataView(sQuery);
        
if  (dvApplyers.Count  >   0 )
        {
            
// 执行查询
            grdRequireList.PageSize  =   500 ;
            grdRequireList.DataSource 
=  dvApplyers;
            grdRequireList.CurrentPageIndex 
=  iPageIndex;
            grdRequireList.DataBind();
        }
    }


 

 

你可能感兴趣的:(datagrid)