Solved

Datagridview column not sorting when clicking on the header

Posted on 2012-03-12
6
394 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

627 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