'----------------------------------------------------- ' SORTING THE DATAGRID CONTROL '----------------------------------------------------- 'This uses an existing database to show records in the grid. 'THINGS TO DO BEFORE SORTING WILL WORK '1. Go into the property builder and on the General Tab, set the datasource to "unbound". This clears ' the combo boxes. You see, since they are sorting the columns, we have to be able to change the ' order of the items in the datagrid manually based on which column they click. If you leave it connected ' to a datasource like we usually do, it will always keep getting the data based on the original SQl ' you set up in the data adapter wizard. '2. Go into your code and create a new event handler for the grid called DataGrid1_SortCommand. '3. Add the code you see in this event below. '4. Finally, change your code in the Page_load to look like mine. This tells the form to fill the grid with the ' original data from the DB the 1st time it is rendered. That is what DefaultView does. Remember that ' datasets keep track of original and changed data. 'Updated 10/1/05 Public Class WebForm1 Inherits System.Web.UI.Page Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load OleDbDataAdapter1.Fill(DataSet11, "customers") DataGrid1.DataSource = DataSet11.Customers.DefaultView DataGrid1.DataBind() End Sub Private Sub DataGrid1_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles DataGrid1.SortCommand '---this easiest way to handle sorting is to use a DataView object. This lets us change how the ' grid will show its data by grabbing data from the dataset and modifying the view based on ' which column they click. ' The column they clicked is passed to this sub by e.SortExpression. This will be "LastName" if they ' click the lastname column or "FirstName" if they clicked that column, etc. Put a breakpoint on SortView= and ' you will see what I mean. It uses the sortexpression you set in the Property Builder's General Tab. Usually I just ' leave it as the name of the columns. Check out that page in Prob. Builder again to see what I mean. '---create a new dataview object and set it to get the original data from the dataset Dim SortView As DataView = DataSet11.Customers.DefaultView '---now tell it to sort by the column they clicked SortView.Sort = e.SortExpression '---tell the grid where to get its data and rebind to make it display the way they want. DataGrid1.DataSource = SortView DataGrid1.DataBind() End Sub Private Sub DataGrid1_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid1.PageIndexChanged DataGrid1.DataSource = DataSet11.Customers OleDbDataAdapter1.Fill(DataSet11, "customers") DataGrid1.CurrentPageIndex = e.NewPageIndex DataGrid1.DataBind() End Sub End Class