No default sort using gridview
No default sort using gridview
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
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
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?
We would need to see that, as Kevin said.
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
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.
This is how it looks when I put it into a gridview that I then convert to a datatable:
Here's the script for converting from gridview (named gvResults) to datatable:
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
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:
The
order
option needs to be outside the ``-option columnDefs` option. It should look like this:Kevin
That's it! Thank you!