views:

46

answers:

1

Dear All,

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();
    }
}
+3  A: 

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 ?

this. __curious_geek
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...
Giuseppe
Show us some code. Edit your question and add some code.
this. __curious_geek
the editor does not like "<", ">"how do I put the code inside my post?
Giuseppe
read it here - http://stackoverflow.com/editing-help
this. __curious_geek
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
Giuseppe
I think I found it. There was a SILLY response.redirect!!! Thanks to averyone.
Giuseppe