我在.aspx上存在一个DataGrid1,该DataGrid1设为"在运行时自动生成列",我在属性中将AllowSorting设为了True,之后DataGrid1的每个列标题上加上了下划线,运行后也可以点击了,但还是没有做出排序来.该如何去实现呢?

解决方案 »

  1.   

    在datagrid 中有事件 sortcommand  在其中添加代码:string SortField=(string)e.SortExpression;
    datatable.DefaultView.Sort=SortField; 
    再绑定此datatable。
    简单的
      

  2.   

    Create an event handler for the grid's SortCommand event. In the method: 
    Get the sort key (the value you set in the Sort expression) from the SortExpression property of the DataGridSortCommandEventArgs argument. 
    Sort the data. 
    Rebind the data from the data source. 
    The following example shows how to sort by handling the SortCommand event. The method gets the sort key value and uses it to set the Sort property of a DataView object. The code then rebinds the grid to the DataView object, which automatically returns the data in sorted order. ' Visual Basic
    Private Sub DataGrid1_SortCommand(ByVal source As Object, _
    ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) _
    Handles DataGrid1.SortCommand
       DataView1.Sort = e.SortExpression
       DataGrid1.DataBind()
    End Sub
      

  3.   

    也可以在dataset的dataview里排序后再把dataset绑定到grid上!