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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.