2

I have a web application (with C#). I have a GridView and want to be able to sort its contents. I have added the tags

...

AllowSorting="True"
onsorting="MyGridView_Sorting">

and

asp:BoundField DataField="NAME" HeaderText="Name" SortExpression="NAME" 

inside the GridView. I have implemented the MyGridView_Sorting method. Thing is: it does not work. Does nothing. The header text "Name" looks like an active link, but clicking produces no effect. Putting a break point inside

MyGridView_Sorting

shows that it actually never goes inside the function. What is wrong? What do I miss?

Thanks!!!

Sep

< asp:GridView ID="MyGridView"
runat="server"                               
CssClass="pvgrid" 
Width="90%" 
AutoGenerateColumns="false"
OnRowCommand="MyGridView_RowCommand"
AllowPaging="True" 
PageSize="10" 
AllowSorting="True"
onsorting="MyGridView_Sorting" 
onpageindexchanging="MyGridView_PageIndexChanging" >

< Columns >
< asp:BoundField DataField="NAME" HeaderText="Name" SortExpression="NAME"  />
< /Columns >
< /asp:GridView >


protected void MyGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    MyGridView.PageIndex = e.NewPageIndex;
    MyGridView.DataBind();
}

protected void MyGridView_Sorting(object sender, GridViewSortEventArgs e)
{
    DataTable dataTable = MyGridView.DataSource as DataTable;

    if (dataTable != null)
    {
        DataView dataView = new DataView(dataTable);
        dataView.Sort = e.SortExpression + " "+ ConvertSortDirectionToSql(e.SortDirection);
        MyGridView.DataSource = dataView;
        MyGridView.DataBind();
    }
}

1 Answer 1

3

GridView delegates sorting to underlying DataSource. GridView does not perform sorting on its own, it just delegates. So you need to look into your DataSource for sorting. What is the DataSource ? What kind of Data does it display ? Are those custom objects coming from ObjectDataSource or you're using SqlDataSource or LinqDataSource ?

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

3 Comments

I populate the data source with a custom function that interrogates the database and binds a dataset to the gridview. What I do not understand is why the application does not even get inside my sorting function. Not even the first line. The function is not called at all. I am sure I am not understanding properly the logic of this business...
the editor does not like "<", ">" how do I put the code inside my post?
what I would like to understand first is why clicking on the bound field with a sort expression does not make the application call the sorting method

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.