Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Set the gridView.AllowSorting property to true. From here the grid should allow you to sort data automatically on postback if you are using an object that implements IBindingList. However, since that is most likely not the case, you should take TheTXI's advice above and handle the sorting event yourself. Either wire the GridView.Sorting event in the codebehind, like so:</p> <pre><code>gridView.Sorting += new GridViewSortEventHandler(gridView_Sorting); </code></pre> <p>Handle the sorting inside the gridView_Sorting method, which should look like this:</p> <pre><code>private void gridView_Sorting(object sender, GridViewSortEventArgs e) { //Sorting logic here } </code></pre> <p>Also, you can wire the event on the page itself using OnSort="gridView_Sorting" attached to the control.</p> <p>Remember, since you are setting gridView.EnableSortingAndPagingCallbacks to false, this will not be immediately fired when the user tries to sort, it instead will wait for the postback to the server.</p> <p>I hope this helps!</p> <p><strong>EDIT:</strong> </p> <p>Since ObjectDataSource seems to be the middleman of choice, here is a brief explanation of wiring that for sorting as well. Use the following in your page (The full example can be found <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.objectdatasource.sortparametername.aspx" rel="noreferrer">here</a> on the MSDN, near the bottom):</p> <pre><code>&lt;asp:GridView ID="TestGridView" runat="server" DataSourceID="ObjectDataSourceTest" AllowSorting="True"&gt; &lt;/asp:GridView&gt; &lt;asp:ObjectDataSource ID="ObjectDataSourceTest" runat="server" SelectMethod="SelectMethod" TypeName="Samples.AspNet.CS.SortingData" SortParameterName="sortExpression"&gt; &lt;/asp:ObjectDataSource&gt; </code></pre> <p>Instead of actually using the gridView.Sorting event, you'll be jumping over to the ObjectDataSource to take care of the sorting. Once the sort is triggered it should call the method found in SelectMethod in your code behind. Then, inside SelectMethod, you would handle the rebuilding of your GridView object, which would look like:</p> <pre><code>public void SelectMethod(string sortExpression) { //Rebuild gridView table if necessary, same method used in //on a postback, and retrieve data from the database. Once //completed sort the data with: gridView.Sort(sortExpression, SortDirection.(Ascending or Descending)) } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload