Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6789231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:34:45+00:00 2026-05-26T17:34:45+00:00

I have a DataGridView in my form and I am binding that DataGridView to

  • 0

I have a DataGridView in my form and I am binding that DataGridView to a List.
The column is set to SortMode=Automatic.

This is my code

List<string> lstLines = new List<string>();
StreamReader sr = new StreamReader(tbFile.Text);
while ((line = sr.ReadLine()) != null)
{
  lstLines.Add(line);
}
dgvLines.DataSource = lstLines.Select(x=>new {Text=x}).ToList();

When I click on column header I expected the column to be sorted but nothing happens.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:34:45+00:00Added an answer on May 26, 2026 at 5:34 pm

    you Can try this……

    Complete code to sort the column of datagridview whose datasource is a generic List

    //—————————————————————————————–
    //In the form – In constructor or form load, populate the grid.
    //——————————————————————————————–

        List<student> students;
    
        private void PopulateList()
        {
            student std1 = new student("sss", 15, "Female");
            student std2 = new student("ddd", 12, "Male");
            student std3 = new student("zzz", 16, "Male");
            student std4 = new student("qqq", 14, "Female");
            student std5 = new student("aaa", 11, "Male");
            student std6 = new student("lll", 13, "Female");
    
            students = new List<student>();
            students.Add(std1);
            students.Add(std2);
            students.Add(std3);
            students.Add(std4);
            students.Add(std5);
            students.Add(std6);
    
            dataGridView1.DataSource = students;
        }
    

    //———————————————————————————————

    //Comparer class to perform sorting based on column name and sort order
    //———————————————————————————————

    class StudentComparer : IComparer<Student>
    {
        string memberName = string.Empty; // specifies the member name to be sorted
        SortOrder sortOrder = SortOrder.None; // Specifies the SortOrder.
    
        /// <summary>
        /// constructor to set the sort column and sort order.
        /// </summary>
        /// <param name="strMemberName"></param>
        /// <param name="sortingOrder"></param>
        public StudentComparer(string strMemberName, SortOrder sortingOrder)
        {
            memberName = strMemberName;
            sortOrder = sortingOrder;
        }
    
        /// <summary>
        /// Compares two Students based on member name and sort order
        /// and return the result.
        /// </summary>
        /// <param name="Student1"></param>
        /// <param name="Student2"></param>
        /// <returns></returns>
        public int Compare(Student Student1, Student Student2)
        {
            int returnValue = 1;
            switch (memberName)
            {
                case "Name" :
                    if (sortOrder == SortOrder.Ascending)
                    {
                        returnValue = Student1.Name.CompareTo(Student2.Name);
                    }
                    else
                    {
                        returnValue = Student2.Name.CompareTo(Student1.Name);
                    }
    
                    break;
                case "Sex":
                    if (sortOrder == SortOrder.Ascending)
                    {
                        returnValue = Student1.Sex.CompareTo(Student2.Sex);
                    }
                    else
                    {
                        returnValue = Student2.Sex.CompareTo(Student1.Sex);
                    }
                    break;
                default:
                    if (sortOrder == SortOrder.Ascending)
                    {
                        returnValue = Student1.Name.CompareTo(Student2.Name);
                    }
                    else
                    {
                        returnValue = Student2.Name.CompareTo(Student1.StudentId);
                    }
                    break;
            }
            return returnValue;
        }
    }
    

    //———————————————————————————————

    // Performing sort on click on Column Header
    //———————————————————————————————

        private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            //get the current column details
            string strColumnName = dataGridView1.Columns[e.ColumnIndex].Name;
            SortOrder strSortOrder = getSortOrder(e.ColumnIndex);
    
            students.Sort(new StudentComparer(strColumnName, strSortOrder));
            dataGridView1.DataSource = null;
            dataGridView1.DataSource = students;
            customizeDataGridView();
            dataGridView1.Columns[e.ColumnIndex].HeaderCell.SortGlyphDirection = strSortOrder;
        }
    
       /// <summary>
        /// Get the current sort order of the column and return it
        /// set the new SortOrder to the columns.
        /// </summary>
        /// <param name="columnIndex"></param>
        /// <returns>SortOrder of the current column</returns>
        private SortOrder getSortOrder(int columnIndex)
        {
            if (dataGridView1.Columns[columnIndex].HeaderCell.SortGlyphDirection == SortOrder.None ||
                dataGridView1.Columns[columnIndex].HeaderCell.SortGlyphDirection == SortOrder.Descending)
            {
                dataGridView1.Columns[columnIndex].HeaderCell.SortGlyphDirection = SortOrder.Ascending;
                return SortOrder.Ascending;
            }
            else
            {
                dataGridView1.Columns[columnIndex].HeaderCell.SortGlyphDirection = SortOrder.Descending;
                return SortOrder.Descending;
            }
        }
    

    I am hoping that it will helps you…..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form that contains dataGridView, whose coloumn are set to dgrv1.Width =dgrv1.Columns.GetColumnsWidth(DataGridViewElementStates.Visible)+20;
So I have this datagridview that is linked to a Binding source that is
On a Windows form I have an unbound datagridview with 1 user-editable column. A
I have a form with a DataGridView showing a list of customers, and some
I have a datagridview on a form with some data. The 1st column contains
I have a DatagridView control on a Windows form. It's selectionMode property is set
I have this problem: I have a datagridview that reads the data from a
I have a DataGridView that shows list of records and when I hit a
hi i have a datagridview in a form... users by clicking the column name
I work with windows forms and on the form I have a DataGridView. This

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.