Solved

Datagridview column not sorting when clicking on the header

Posted on 2012-03-12
6
376 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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

12 Experts available now in Live!

Get 1:1 Help Now