No default sort using gridview

No default sort using gridview

kkittell518kkittell518 Posts: 16Questions: 4Answers: 0

I use a gridview then convert that to a datatable with the script.
<asp:GridView ID="gvResults" runat="server" OnRowDataBound="gvResults_RowDataBound" CssClass="stripe row-border responsive no-wrap" Style="max-width: 100%" GridLines="None"></asp:GridView> $('#<%= gvResults.ClientID %>').DataTable()

I've tried a few ways to disable the default sort on the first column that don't work, including:

```columnDefs: [
{
"aaSorting": []
}
]

and order[]

If I don't convert the gridview to a datatable, it respects the ordering from the sql. Is there anything else that would work?

Thank you,
Karen

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 14,171Questions: 25Answers: 3,343

    Using order: [] will order the table based on the order it is populated into the Datatable. See this example:
    http://live.datatables.net/kimofeti/1/edit

    If the order option isn't working as you expect please post a link to your page or a test case replicating the issue so we can help debug.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • kkittell518kkittell518 Posts: 16Questions: 4Answers: 0

    The page isn't publicly available, and I don't see how I can use any of the available methods for posting the test case because I use c# code to load the asp:Gridview, then use the jquery to convert it to a datable, as above.

    This is the c# rowDataBound code:
    protected void gvResults_RowDataBound(object sender, GridViewRowEventArgs e) { //check if the row is the header row if (e.Row.RowType == DataControlRowType.Header) { //add the thead and tbody section programatically e.Row.TableSection = TableRowSection.TableHeader; } for (int i = 0; i < e.Row.Cells.Count; i++) { if (e.Row.RowType == DataControlRowType.DataRow) { string decodedText = HttpUtility.HtmlDecode(e.Row.Cells[i].Text); e.Row.Cells[i].Text = decodedText; } } }

    Any thoughts?

  • colincolin Posts: 12,939Questions: 0Answers: 2,204

    We would need to see that, as Kevin said.

    I use c# code to load the asp:Gridview, then use the jquery to convert it to a datable

    All we need to see is the data and that conversion to a DataTable - where that data comes from isn't important for this query. So if you could update Kevin's example, please, we should be able to progress this,

    Colin

  • kkittell518kkittell518 Posts: 16Questions: 4Answers: 0
    edited October 7

    Here's an example of the data. The sql says to sort on the Name field. This is the way it's returned to the c# DataTable, I used the same c# DataTable to display in just a gridview and the gridview that I converted to a datatable.

    species_id  Name                            Country 
    315471  Abelmoschus caillei         Benin   
    619         Abelmoschus esculentus  Zimbabwe    
    100044  Senna obtusifolia                   Hispaniola  
    100044  Senna obtusifolia                   Zimbabwe    
    

    This is how it looks when I put it into a gridview that I then convert to a datatable:

    species_id  Name                            Country
    619         Abelmoschus esculentus  Zimbabwe
    100044  Senna obtusifolia                   Hispaniola
    100044  Senna obtusifolia                   Zimbabwe
    315471  Abelmoschus caillei          Benin
    

    Here's the script for converting from gridview (named gvResults) to datatable:

       $(document).ready(function () {
                $('#<%= gvResults.ClientID %>').DataTable({
                dom: 'Bfirtip',
                columnDefs: [
                    {
                        "order": []
                    }
                ],
                buttons: [
                    {
                        extend: 'colvis',
                        text: 'Show/hide columns'
                    },
                    'pageLength', 'excel',
                    {
                        extend: 'csv',
                        customize: function (csv) {
                            // remove no-break space causing problems with Null fields
                            return csv.replace(/\u00A0/g, "");
                        }
                    }
                ],
                language:
                {
                    emptyTable: "Your search returned no results."
                },
                lengthMenu: [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]],
            });
        }); 
    

    In my previous response shows the gridview_rowDataBound, which just loads the gridview from the c# datatable.

    I use this method of converting from c# gridview to jquery datatable often in my application, but the order[] is ignored. Everything else works perfectly.
    I hope this is enough information for you to help me figure out why order is ignored.
    Thank you!

    Edited by Kevin: Syntax highlighting. Details on how to highlight code using markdown can be found in this guide

  • kthorngrenkthorngren Posts: 14,171Questions: 25Answers: 3,343
    edited October 7 Answer ✓

    Thanks for using Markdown to format the code. It didn't work because you need to place the triple ticks on a new line by them selves.

    You have:

             columnDefs: [
                 {
                     "order": []
                 }
             ],
    

    The order option needs to be outside the ``-option columnDefs` option. It should look like this:

             $('#<%= gvResults.ClientID %>').DataTable({
             dom: 'Bfirtip',
             "order": [],
             buttons: [
             .....
    

    Kevin

  • kkittell518kkittell518 Posts: 16Questions: 4Answers: 0

    That's it! Thank you!

Sign In or Register to comment.