3

I have a gridview which is bound to a DataTable. When I try to sort the gridview, it goes blank. How can I enable sorting this gridview?

I know this question has been asked before, but what I'm looking for is an explanation of how to do it. Perhaps with a simple example.

I have read that I need to put some code in the on_sorting and/or on_sorted events, but I don't understand what needs to go there.

Again, I want to understand the method of accomplishing this, I don't just want a giant block of code.


Ok, here's what I have now, but still not working:

            <asp:GridView ID="gridResults" runat="server" CellPadding="4" ForeColor="#333333"
            GridLines="Horizontal" AllowSorting="True" AllowPaging="True" 
            EmptyDataText="No Tracking Information Found for the given criteria." 
            PageSize="15" onsorted="gridResults_Sorted" AutoGenerateColumns="False" 
            EnableSortingAndPagingCallbacks="True">
            <RowStyle BackColor="#E3EAEB" />
            <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#7C6F57" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>

And then in code behind:

  //From search method
gridResults.Columns.Clear();
    foreach (DataColumn col in currentResults.Columns)
    {
        String fieldName = col.ColumnName;
        BoundField field = new BoundField();
        field.DataField = fieldName;
        field.SortExpression = fieldName;
        field.HeaderText = fieldName;

        gridResults.Columns.Add(field);  
    }

    gridResults.DataSource = currentResults;
    gridResults.DataBind();
    gridResults.AllowSorting = true;

Can anyone see what I'm still missing? Results show, but no sorting or paging works.

2 Answers 2

1

You are able to make a GridView sortable using a DataTable. All you need to do is make sure that you have Enable Sorting = true and you provide the column name that you wish to be sorted in the SortExpression.

This is what you would need to do to allow this to be sortable:

<asp:BoundField HeaderText="Product" 
      DataField="ProductName" SortExpression="ProductName">
</asp:BoundField>

You always need to set the SortExpression or you are not going to be able to sort your columns

Sign up to request clarification or add additional context in comments.

5 Comments

Hmm. Ok, but can this be done programmatically? I'm allowing the user to select which columns will exist in the gridview (search results). Can I do this in c# and use something like a foreach on the datatable's columns to get the column names?
Yes, you can grab the column names inside a foreach loop and then set the properties accordingly. Here is a good article to look at: msdn.microsoft.com/en-us/library/…
Also, if you know what the fields are going to be before you make the call, aka between when the user makes the selection and you call for your data, you can create the columns then using the selections and then just assign your data table to your datasource.
@hav2play21 - Which will have better performance, foreach or using the selections? I'm thinking foreach so I don't bother checking the non-selected options.
It really all depends on how you are sending your selections to grab your data. If you are already using the selections to ask for your data then that way would be easier but if you dont know the exact selections already then using a foreach loop after you get your data back would be better.
1

please have a look on GridView Examples for ASP.NET 2.0: Paging and Sorting the GridView's Data

4 Comments

I'm familiar with this information, I'm binding my gridview to a DataTable, not to a datasource.
why you can do it in DataView . do you want example? i think in data table its not possible
The datatable is convenient for other processing I do later. Are you suggesting I cast/convert the datatable to a dataview and bind that to the gridview?
Yes, an example may help. I think I'm simple missing what your saying.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.