Solved

Datagridview column not sorting when clicking on the header

Posted on 2012-03-12
6
385 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

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…

820 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