I need to build a custom sort method for a GridView control?

I need to build a custom sort method for a GridView control, but I'm having trouble properly accessing the grid ID and the sort expression. Can some one give me an example of a custom sort method or links to one? I have to add many grids to my page and they all need to be sortable through one method.


        private void SortAllGrids(object sender, GridViewSortEventArgs e)
        {
            GridView gridView = (GridView)sender;
            string test = gridView.ID;           
        }

Open in new window

LVL 1
Michael SterlingWeb Applications DeveloperAsked:
Who is Participating?
 
Dustin SaundersDirector of OperationsCommented:
Heres the link to the GridView.Sort method:
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.sort(v=vs.110).aspx

Your expression is the string to sort by (csv Column names) and the second override is the SortDirection.

If you look at the method I posted earlier:
        protected void SortGridView(GridView gridView, string expression, string direction)
        {
            SortDirection sortDirection;

            switch (direction)
            {
                case "Asc":
                    sortDirection = SortDirection.Ascending;
                    break;
                case "Desc":
                    sortDirection = SortDirection.Descending;
                    break;
                default:
                    sortDirection = SortDirection.Ascending;
                    break;
            }

            gridView.Sort(expression, sortDirection);
        }

Open in new window


You can sort the gridview of your choice with something like this.  Assume the view has the columns "FirstName","LastName","Email" and I want to sort by "LastName" ascending:
SortGridView(gv,"LastName","Asc");

Open in new window


Now, you probably want to pass in those string values from your HTML page (a radio with ascending or descending and the column to sort) but so you can see the example.
0
 
Dustin SaundersDirector of OperationsCommented:
You can get all the grid views with something like this:

        private List<string> gvIDs = new List<string>();

        protected void GetGridViews()
        {
            foreach (Control c in form1.Controls)
            {
                if (c is GridView)
                {
                    gvIDs.Add(((GridView)c).ID);
                }
            }
        }

Open in new window


Or better, a list of the GridViews:

        private List<GridView> gvs = new List<GridView>();

        protected void GetGridViews()
        {
            foreach (Control c in form1.Controls)
            {
                if (c is GridView)
                {
                    gvs.Add(((GridView)c));
                }
            }
        }

Open in new window


Then on your sort, you have the list of controls to use.
0
 
Michael SterlingWeb Applications DeveloperAuthor Commented:
Not all of the grids will always contain data. I only need to work with the grids that are visible and have data.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
Dustin SaundersDirector of OperationsCommented:
Might be good to include that detail in the first post so you get a more complete answer off the cuff.

You can filter the GridViews added by whatever criteria you use to define "visible" and "have data".  Example:

        protected void GetGridViews()
        {
            foreach (Control c in form1.Controls)
            {
                if (c is GridView)
                    if (((GridView)c).Style.Value != "display: none;" && ((GridView)c).Rows.Count > 0)
                        gvs.Add(((GridView)c));
            }
        }

Open in new window


Then you can use a method and pass your gridviews to it, sorting on whatever criteria.
        protected void SortGridView(GridView gridView, string expression, string direction)
        {
            SortDirection sortDirection;

            switch (direction)
            {
                case "Asc":
                    sortDirection = SortDirection.Ascending;
                    break;
                case "Desc":
                    sortDirection = SortDirection.Descending;
                    break;
                default:
                    sortDirection = SortDirection.Ascending;
                    break;
            }

            gridView.Sort(expression, sortDirection);
        }

Open in new window

0
 
Michael SterlingWeb Applications DeveloperAuthor Commented:
Agreed. I did put your code into place. it doesn't seem to find any of my grid view controls. I tried using Page.Controls in my foreach loop but got nothing. How do I correctly grab my page to make sure I get the controls on it?
0
 
Dustin SaundersDirector of OperationsCommented:
If you're scanning child controls you need to search recursively.

        protected void GetGridViews(Control parent)
        {
            foreach (Control c in parent.Controls)
            {
                if (c is GridView)
                {
                    if (((GridView)c).Style.Value != "display: none;" || ((GridView)c).Rows.Count > 0)
                        gvs.Add(((GridView)c));
                }
                else
                    GetGridViews(c);
            }
        }

Open in new window


And you would call it with:
GetGridViews(Page);

Open in new window

0
 
Michael SterlingWeb Applications DeveloperAuthor Commented:
That worked better. Now I'm still having an issue with calling the grid's sort method and how do I access the sort expression for the grid:

        protected void GetGridViews(Control parent)
        {
            List<GridView> gvs = new List<GridView>();
            foreach (Control c in parent.Controls)
            {
                if (c is GridView)
                {
                    if (((GridView)c).Style.Value != "display: none;" || ((GridView)c).Rows.Count > 0)
                        gvs.Add(((GridView)c));

                    ePassportScheduledEventSearchResultCollection clinics = new ePassportScheduledEventSearchResultCollection().BinaryDeserialize(Session[SessionConstants.CLINIC_SEARCH_RESULTS + strID] as string);
                    clinics.EventSearchResults = c.Sort(clinics.EventSearchResults, (WHAT GOES HERE FOR TO GET THE GridViewSortEventArgrs???) ).ToList();
                    Session[SessionConstants.CLINIC_SEARCH_RESULTS + strID] = clinics.BinarySerialize();
                }
                else
                    GetGridViews(c);
            }
        }

Open in new window


(GridView)c.Sort does not work and I'm getting an error message at compile time telling me:

"Control does not contain a definition for 'Sort' and no extension method 'Sort' accepting a first argument of type 'Control' could be found (are you missing a using directive or an assembly reference?)
0
 
Dustin SaundersDirector of OperationsCommented:
You would need to use
((GridView)c).Sort()

Open in new window


If you don't encapsulate it you can't access the methods of that type.
0
 
Michael SterlingWeb Applications DeveloperAuthor Commented:
Thank you. Turns out, i was using the wrong class and the normal grid sort had been customized/extended for the class that I needed, so it took different parameters.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.