tags:

views:

36

answers:

1

Hi I have a datagridview which binds XML manually.

I wish to sort out the columns by clicking column header.

here is the method i wrote: when you click column header, it grabs column header and sorting data by that column header.

I also put a toggle switch(direction) on that, so when user click again , the data can be sorted in different orders ( ascending/ descending)

public BindingSource BindXML(string file, string headerName, bool direction)
        {
            XElement record = XElement.Load(file);
            var q = from r in record.Descendants("record")
                    //ascending order?
                    orderby (string)r.Element(headerName)
                    select new
                    {
                        work_pack = (int)r.Element("work_pack"),
                        Locational_Details = (string)r.Element("Locational_Details"),
                        RegimeName = (string)r.Element("RegimeName")
                    };
            if (direction)
            {
                //descending order
                q.OrderByDescending(r => r);
            }

           return new BindingSource(q, null);}

The problem is the lambda expression q.OrderByDescending(r => r); doesn't work at all

i even tried q.OrderByDescending(r => r.RegimeName)

and q.OrderByDescending(r => r.Element(headerName));

Neither of them works. Any help on that?

+1  A: 

unlike List<T>.Sort() the IEnumerable<T>.OrderBy() is NOT an in-place sort.
Change your code to q = q.OrderByDescending(r => r.HeaderName); and it should work

liho1eye
well said +1 :)
erash