0

I'm developing a web-part for SharePoint 2010.

I've to sort a SPGridView so I use SortExpression.

I execute a CAML query in this way and then I bind data to SPGridView:

            SPQuery query = new SPQuery();
            query.RowLimit = 30;
            query.Query = "<OrderBy><FieldRef Name=\"" + sortColumn + "\" Ascending=\"" + sortDir + "\" /></OrderBy>";
            query.ViewFields = "<FieldRef Name=\"Title\" />" +
                               "<FieldRef Name=\"DocIcon\" />" +
                               "<FieldRef Name=\"Ragione_x0020_Sociale\" />" +
                               "<FieldRef Name=\"Tipo_x0020_Protocollo\" />" +
                               "<FieldRef Name=\"Data_x0020_Protocollo\" />" +
                               "<FieldRef Name=\"Formato_x0020_Documento\" />" +
                               "<FieldRef Name=\"Utilizzatore_x0020_Interno\" />" +
                               "<FieldRef Name=\"Note\" />" +
                               "<FieldRef Name=\"Descrizione\" />";

            //query.QueryThrottleMode = SPQueryThrottleOption.Override;
            string listName = "Protocolli";
            SPList list = SPContext.Current.Web.Lists[listName];
            //list.EnableThrottling = false;

            if (!string.IsNullOrEmpty(pagingInfo))
            {
                SPListItemCollectionPosition position = new SPListItemCollectionPosition(pagingInfo);
                query.ListItemCollectionPosition = position;
            }

            SPListItemCollection collection = list.GetItems(query);
            grdProtocolli.DataSource = collection.GetDataTable();

            grdProtocolli.PagerTemplate = null;  // Must be called after Controls.Add(oGrid)
            grdProtocolli.DataBind();

sortColumn is:

if (ViewState["SortExpression"] == null)
            {
                sortColumn = "Data_x0020_Protocollo";
            }
            else
            {
                sortColumn = ViewState["SortExpression"].ToString();
            }

Data_x0020_Protocollo is a field that currently exists.

Now, trying to execute my web-part I receive this error:

0x0AB0 SharePoint Foundation Runtime tkau Unexpected System.ArgumentException: Impossibile trovare un campo con il nome "Data_x0020_Protocollo". at Microsoft.SharePoint.SPFieldCollection.GetFieldByDisplayName(String strDisplayName, Boolean bThrowException)

"Impossibile trovare un campo con il nome..." is translated like "Impossible to find a field with name"

If I use the display name Data Protocollo I don't get the error anymore (and I can see the GridView), but sorting is not working.

Any suggestions?

1 Answer 1

0

Couple of things.

  1. You need to handle OnSorting event and manually sort the DataTable. Then put the datatable back into ViewState
  2. You should use DisplayName

Pseudo Code

protected void grdProtocolli_Sorting(object sender, GridViewSortEventArgs e)  
{  
    string sortingDirection = //LOGIC TO SET DIRECTION

    //direction is the public property
    if (direction == SortDirection.Ascending)  
    {  
        direction = SortDirection.Descending;  
        sortingDirection = "Desc";
    }  
    else  
    {  
        direction = SortDirection.Ascending;  
        sortingDirection = "Asc";         
    }  

    DataView sortedView = //Get DataView from DataTable.. You might need to read it from either ViewState or directly from the Grid View Control
    sortedView.Sort = e.SortExpression + " " + sortingDirection;  
    Session["Next"] = sortedView;  
    grdProtocolli.DataSource = sortedView;  
    grdProtocolli.DataBind();  
}  
2
  • Thanks for the reply @Amal Hashim. This is my OnSorting event pastebin.com/fcQFxHkc I don't think to have understood well the "put the datatable back into ViewState". Could you please provide an example?
    – Pepozzo
    May 11, 2015 at 15:07
  • @Pepozzo see my updated answer. The code is not tested, but it will give you the general idea. May 11, 2015 at 15:12

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.