Solved

Datagridview column not sorting when clicking on the header

Posted on 2012-03-12
6
391 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

728 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