Solved

Datagridview column not sorting when clicking on the header

Posted on 2012-03-12
6
382 Views
Last Modified: 2012-04-07
Hi

I have a datagridview that is populated with information.

I thought that clicking on header automatically sorts the information.

I have allowUserToOrderColumns set to true.

Is it because I am using linq and saving the information to a var rather than a datatable?

populating the information using linq
            dbHCAEntities context = new dbHCAEntities();

            var fam = from a in context.mfFamily
                      where a.memno == memno
                      orderby a.memno
                      select new
                      {
                          Title = a.title,
                          LastName = a.namel,
                          FirstName = a.namef,
                          famid = a.famid
                      };

            dgFamily.DataSource = fam;

Open in new window

0
Comment
Question by:elmbrook
  • 3
  • 3
6 Comments
 
LVL 11

Expert Comment

by:rowansmith
ID: 37713044
AllowUserToOrderColumns is unrelated - this allows the user to drag the order of the columns.

Check that you have set the  DataGridViewColumn.SortMode Property on the column you are trying to sort on.
0
 

Author Comment

by:elmbrook
ID: 37717464
dgFamily.Columns[0].SortMode = DataGridViewColumnSortMode.Automatic;
dgFamily.Columns[1].SortMode = DataGridViewColumnSortMode.Automatic;
dgFamily.Columns[2].SortMode = DataGridViewColumnSortMode.Automatic;
dgFamily.Columns[3].SortMode = DataGridViewColumnSortMode.Automatic;

Open in new window


Does not work. I still cannot click on the headers to sort.

ok, I got a little frustrated so I quickly created a datatable and attached it to a datagrid. This was able to be sorted.

How can I get my Linq query to work?

            DataTable dt = new DataTable();

            DataColumn column;

            column = new DataColumn("name", typeof(String)); // 0
            dt.Columns.Add(column);

            DataRow newrow = dt.NewRow();
            newrow["name"] = "AAA";
            dt.Rows.Add(newrow);
            newrow = dt.NewRow();
            newrow["name"] = "BBB";
            dt.Rows.Add(newrow);
            newrow = dt.NewRow();
            newrow["name"] = "CCC";
            dt.Rows.Add(newrow);
            newrow = dt.NewRow();
            newrow["name"] = "DDD";
            dt.Rows.Add(newrow);

            d1.DataSource = dt;

Open in new window

0
 
LVL 11

Expert Comment

by:rowansmith
ID: 37717964
Hmm.. I am having the same problem.  Worse, under certain circumstances I can not get any Colomns to be displayed from the LINQ DataSource.  There is an enormous amount of "chatter" on the Internet about DataGridView and LINQ but I really could not find anything useful.  Sorry, I have to bow out.

Hopefully someone else can assist you.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Accepted Solution

by:
elmbrook earned 0 total points
ID: 37717986
I think I may need to go to using a DataTable to get around this issue.

Anyone else that can help?
0
 
LVL 11

Assisted Solution

by:rowansmith
rowansmith earned 500 total points
ID: 37718024
Yeah I agree.  Sorry I can not help you.  I spent a couple of hours creating queries and binding the DataGridView to the output with really weird and mixed results...

Good luck.
0
 

Author Closing Comment

by:elmbrook
ID: 37818727
No Usable Solutions
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now