C#中已排序DataTable绑定GridView后显示顺序不一致求助
Hey there, sorry you're running into this issue—when your pre-sorted DataTable doesn't match what's displayed in the GridView, it's usually a small setting or overlooked detail causing the mismatch. Let's walk through the most common fixes based on your code:
1. Verify your DataTable is actually sorted before binding
First, let's confirm the FromDataTable() method is returning a properly sorted DataTable. Sometimes we assume the sort is applied, but it might not be. Add a quick check right before binding to validate:
DataTable dtColumns = FromDataTable(); // Quick debug to check the current sort (or inspect the first few rows) Debug.WriteLine($"Current DataTable sort: {dtColumns.DefaultView.Sort}"); GridView1.DataSource = dtColumns; GridView1.DataBind();
If the sort is empty or not what you expect, explicitly set the sort on the DataTable's DefaultView before binding:
dtColumns.DefaultView.Sort = "YourTargetColumnName ASC"; // Replace with your column and direction GridView1.DataSource = dtColumns.DefaultView; // Bind the sorted view instead of the raw table GridView1.DataBind();
2. Disable GridView's built-in sorting entirely
Looking at your ASPX markup, you have AllowSorting="true" set, even though you mentioned disabling the sorting event. Even without handling OnSorting, enabling AllowSorting can make the GridView respect ViewState for sort order, which might override your DataTable's pre-sort. Switch this to false:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" ShowHeaderWhenEmpty="true" EmptyDataText="No Records Found" OnRowDataBound="GridView1_RowDataBound" AllowSorting="false" <!-- Update this from true to false --> OnPageIndexChanging="GridView1_PageIndexChanging" AllowPaging="true" CellPadding="1" PageSize="200" AutoPostBack="true" AllowCustomPaging="true" ShowHeader="true" CellSpacing="1" BackColor="#e7e7e8" BorderColor="#e7e7e8" GridLines="Both" CssClass="GridViewStyleB" Font-Names="Calibri" Font-Size="10pt"> <!-- Rest of your GridView markup --> </asp:GridView>
3. Check for ViewState interference
Since your GridView has AutoPostBack="true", ViewState might be holding onto a previous sort order from a prior postback. Try clearing ViewState temporarily before binding:
// Disable ViewState temporarily to avoid retained sort settings GridView1.EnableViewState = false; DataTable dtColumns = FromDataTable(); GridView1.DataSource = dtColumns; GridView1.DataBind(); GridView1.EnableViewState = true; // Re-enable if you need it for paging or other features
If you don't rely on ViewState for the GridView at all, you can just set EnableViewState="false" directly in the markup.
4. Validate your custom paging logic
You have AllowCustomPaging="true", which means your GridView1_PageIndexChanging event handler is responsible for fetching the correct page of data. Make sure this code isn't re-sorting the data or ignoring the pre-sorted DataTable's order. When slicing data for paging, use the already sorted DataTable/View to extract the page rows—don't re-run a query that might reset the sort.
Give these steps a try, and hopefully one of them gets your GridView showing the correct order!
内容的提问来源于stack exchange,提问作者vicky

