GridView.SortExpression Property

GridView.SortExpression Property
Gets the sort expression associated with the column or columns being sorted. 
Namespace: System.Web.UI.WebControls
Assembly: System.Web (
in  system.web.dll)

Syntax

Visual Basic (Declaration)
Public Overridable ReadOnly Property SortExpression As String
Visual Basic (Usage)
Dim instance As GridView
Dim value As String

value 
=  instance.SortExpression

C#
public   virtual   string  SortExpression  get; }
C
++
public :
virtual  property String ^  SortExpression  {
    String
^ get ();
}

J#
/**/ /** @property */
public  String get_SortExpression ()

JScript
public  function  get  SortExpression () : String

XAML
Not applicable.


Property Value
The sort expression associated with the column or columns being sorted. 
Remarks

Use the SortExpression property to determine the sort expression associated with the column or columns being sorted.

Note:  
The GridView control has a built
- in  sorting feature that automatically sets  this  property. This property  is  typically used only when you need to programmatically determine the column or columns being sorted or when you are adding your own custom sorting functionality to a GridView control.
 

When multiple columns are sorted, 
this  property contains a comma - separated list of the fields by which to sort.

Example

The following code example demonstrates how to use the SortExpression property to determine the name of the column being sorted.

Visual Basic Copy Code
<% @ Page language = " VB "   %>

<! DOCTYPE html PUBLIC  " -//W3C//DTD XHTML 1.0 Transitional//EN "
    
" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
< script runat = " server " >

  Sub CustomersGridView_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    
    
'  Use the RowType property to determine whether the 
     '  row being created is the header row. 
    If e.Row.RowType  =  DataControlRowType.Header Then
    
      
'  Call the GetSortColumnIndex helper method to determine
       '  the index of the column being sorted.
      Dim sortColumnIndex As Integer  =  GetSortColumnIndex()
      
      If sortColumnIndex 
<>   - 1  Then
      
        
'  Call the AddSortImage helper method to add
         '  a sort direction image to the appropriate
         '  column header. 
        AddSortImage(sortColumnIndex, e.Row)
    
      End If
      
    End If
    
  End Sub

  
'  This is a helper method used to determine the index of the
   '  column being sorted. If no column is being sorted, -1 is returned.
  Function GetSortColumnIndex() As Integer

    
'  Iterate through the Columns collection to determine the index
     '  of the column being sorted.
    Dim field As DataControlField
    For Each field In CustomersGridView.Columns
    
      If field.SortExpression 
=  CustomersGridView.SortExpression Then
      
        Return CustomersGridView.Columns.IndexOf(field)

      End If
      
    Next

    Return 
- 1
      
  End Function

  
'  This is a helper method used to add a sort direction
   '  image to the header of the column being sorted.
  Sub AddSortImage(ByVal columnIndex As Integer, ByVal row As GridViewRow)

    
'  Create the sorting image based on the sort direction.
    Dim sortImage As New Image()
    If CustomersGridView.SortDirection 
=  SortDirection.Ascending Then
    
      sortImage.ImageUrl 
=   " ~/Images/Ascending.jpg "
      sortImage.AlternateText 
=   " Ascending Order "
    
    Else
    
      sortImage.ImageUrl 
=   " ~/Images/Descending.jpg "
      sortImage.AlternateText 
=   " Descending Order "
    
    End If
    
'  Add the image to the appropriate header cell.
    row.Cells(columnIndex).Controls.Add(sortImage)
    
  End Sub
    
</ script >

< html   >
  
< head runat = " server " >
    
< title > GridView AllowSorting Example </ title >
</ head >
< body >
    
< form id = " form1 "  runat = " server " >
        
      
< h3 > GridView AllowSorting Example </ h3 >

      
< asp:gridview id = " CustomersGridView "  
        datasourceid
= " CustomersSource "  
        autogeneratecolumns
= " false "
        emptydatatext
= " No data available. "  
        allowsorting
= " true "
        onrowcreated
= " CustomersGridView_RowCreated "
        runat
= " server " >
        
        
< columns >
          
< asp:boundfield datafield = " CustomerID "
            headertext
= " Customer ID "
            headerstyle
- wrap = " false "  
            sortexpression
= " CustomerID " />
          
< asp:boundfield datafield = " CompanyName "
            headertext
= " CompanyName "
            headerstyle
- wrap = " false "
            sortexpression
= " CompanyName " />
          
< asp:boundfield datafield = " Address "
            headertext
= " Address "
            headerstyle
- wrap = " false "
            sortexpression
= " Address " />
          
< asp:boundfield datafield = " City "
            headertext
= " City "
            headerstyle
- wrap = " false "
            sortexpression
= " City " />
          
< asp:boundfield datafield = " PostalCode "
            headertext
= " Postal Code "
            headerstyle
- wrap = " false "
            sortexpression
= " PostalCode "   />
          
< asp:boundfield datafield = " Country "
            headertext
= " Country "
            headerstyle
- wrap = " false "
            sortexpression
= " Country " />          
        
</ columns >
                
      
</ asp:gridview >
            
      
<!--  This example uses Microsoft SQL Server and connects   -->
      
<!--  to the Northwind sample database. Use an ASP.NET      -->
      
<!--  expression to retrieve the connection  string  value    -->
      
<!--  from the Web.config file.                             -->
      
< asp:sqldatasource id = " CustomersSource "
        selectcommand
= " Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers] "
        connectionstring
= " <%$ ConnectionStrings:NorthWindConnectionString%> "  
        runat
= " server " />
        
    
</ form >
  
</ body >
</ html >


C# Copy Code
<% @ Page language = " C# "   %>

<! DOCTYPE html PUBLIC  " -//W3C//DTD XHTML 1.0 Transitional//EN "
    
" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
< script runat = " server " >

  
void  CustomersGridView_RowCreated(Object sender, GridViewRowEventArgs e)
  
{
    
    
// Use the RowType property to determine whether the 
    
// row being created is the header row. 
    if (e.Row.RowType == DataControlRowType.Header)
    
{
      
// Call the GetSortColumnIndex helper method to determine
      
// the index of the column being sorted.
      int sortColumnIndex = GetSortColumnIndex();
      
      
if (sortColumnIndex != -1)
      
{
        
// Call the AddSortImage helper method to add
        
// a sort direction image to the appropriate
        
// column header. 
        AddSortImage(sortColumnIndex, e.Row);
      }

    }

  }


  
//  This is a helper method used to determine the index of the
  
//  column being sorted. If no column is being sorted, -1 is returned.
   int  GetSortColumnIndex()
  
{

    
// Iterate through the Columns collection to determine the index
    
// of the column being sorted.
    foreach (DataControlField field in CustomersGridView.Columns)
    
{
      
if (field.SortExpression == CustomersGridView.SortExpression)
      
{
        
return CustomersGridView.Columns.IndexOf(field);
      }

    }


    
return -1;
  }


  
//  This is a helper method used to add a sort direction
  
//  image to the header of the column being sorted.
   void  AddSortImage( int  columnIndex, GridViewRow headerRow)
  
{
    
    
// Create the sorting image based on the sort direction.
    Image sortImage = new Image();
    
if (CustomersGridView.SortDirection == SortDirection.Ascending)
    
{
      sortImage.ImageUrl 
= "~/Images/Ascending.jpg";
      sortImage.AlternateText 
= "Ascending Order";
    }

    
else
    
{
      sortImage.ImageUrl 
= "~/Images/Descending.jpg";
      sortImage.AlternateText 
= "Descending Order";
    }


    
// Add the image to the appropriate header cell.
    headerRow.Cells[columnIndex].Controls.Add(sortImage);
    
  }

    
</ script >

< html   >
  
< head runat = " server " >
    
< title > GridView AllowSorting Example </ title >
</ head >
< body >
    
< form id = " form1 "  runat = " server " >
        
      
< h3 > GridView AllowSorting Example </ h3 >

      
< asp:gridview id = " CustomersGridView "  
        datasourceid
= " CustomersSource "  
        autogeneratecolumns
= " false "
        emptydatatext
= " No data available. "  
        allowsorting
= " true "
        onrowcreated
= " CustomersGridView_RowCreated "
        runat
= " server " >
        
        
< columns >
          
< asp:boundfield datafield = " CustomerID "
            headertext
= " Customer ID "
            headerstyle
- wrap = " false "  
            sortexpression
= " CustomerID " />
          
< asp:boundfield datafield = " CompanyName "
            headertext
= " CompanyName "
            headerstyle
- wrap = " false "
            sortexpression
= " CompanyName " />
          
< asp:boundfield datafield = " Address "
            headertext
= " Address "
            headerstyle
- wrap = " false "
            sortexpression
= " Address " />
          
< asp:boundfield datafield = " City "
            headertext
= " City "
            headerstyle
- wrap = " false "
            sortexpression
= " City " />
          
< asp:boundfield datafield = " PostalCode "
            headertext
= " Postal Code "
            headerstyle
- wrap = " false "
            sortexpression
= " PostalCode "   />
          
< asp:boundfield datafield = " Country "
            headertext
= " Country "
            headerstyle
- wrap = " false "
            sortexpression
= " Country " />          
        
</ columns >
                
      
</ asp:gridview >
            
      
<!--  This example uses Microsoft SQL Server and connects   -->
      
<!--  to the Northwind sample database. Use an ASP.NET      -->
      
<!--  expression to retrieve the connection  string  value    -->
      
<!--  from the Web.config file.                             -->
      
< asp:sqldatasource id = " CustomersSource "
        selectcommand
= " Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers] "
        connectionstring
= " <%$ ConnectionStrings:NorthWindConnectionString%> "  
        runat
= " server " />
        
    
</ form >
  
</ body >
</ html >


转载于:https://www.cnblogs.com/adam/archive/2007/10/19/930734.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值