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

  • Home
  • SEARCH
  • 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 6348531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:26:18+00:00 2026-05-24T21:26:18+00:00

I have a DataGridView with 6 columns. Example: column1 column2 column3 column4 column5 column6

  • 0

I have a DataGridView with 6 columns.

Example:

column1    column2    column3    column4    column5    column6
J6         RES-0112G  123.123    456.456    180        1111
FID2       FIDUCIAL   5.123     -50.005     90         FIDUCIAL
R100       RES-0113G  1.1       -123.123    90         1111
C12        CAP-1234H -99.99     -987.123    45         2222
Q1         CAP-1234Z -99.99     -987.123    45         4444
J3         RES-0112G  123.123    999.999    0          1111
FID1       FIDUCIAL   23.123     23.123     0          FIDUCIAL
F1         CAP-1234  -88.99     -555.111    45         DDDD
C11        CAP-1234Z -123.99    -123.123    270        abc2222

And I would like to sort it in a special order. Let’s say I want to sort it by the last value (column 6) in this sequence:

FIDUCIAL, 1111, 2222, DDDD, 4444

AND then sort it secondly by the 2nd column alpha-numerically. (NOTE THE abc2222 sorts by “2222” not “abc”).

So the updated DataGridView would look like this: (For the FIDUCIALS I would like to sort by column 1 instead of column 2)

column1    column2    column3    column4    column5    column6
FID1       FIDUCIAL   23.123     23.123     0          FIDUCIAL
FID2       FIDUCIAL   5.123     -50.005     90         FIDUCIAL
J6         RES-0112G  123.123    456.456    180        1111
J3         RES-0112G  123.123    999.999    0          1111
R100       RES-0113G  1.1       -123.123    90         1111
C11        CAP-1234C -123.99    -123.123    270        abc2222
C12        CAP-1234H -99.99     -987.123    45         2222
F1         CAP-1234  -88.99     -555.111    45         DDDD
Q1         CAP-1234Z -99.99     -987.123    45         4444

Does anyone know how to properly sort this? I am using a SortableBindingList<>

  • 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-24T21:26:19+00:00Added an answer on May 24, 2026 at 9:26 pm

    I’ve created a sample code to show you the technique itself. I hope you will not find it complicated to adapt to your needs. The basic idea is that you group by required column and then apply custom sort inside the group. I didn’t grasp the sorting algorithm for column6, so I’ve made a simple sorting based on your sample data. Hope it helps!

    class Program
    {
        public class Row
        {
            public string Column1 { get; set; }
            public string Column2 { get; set; }
            public string Column3 { get; set; }
            public string Column4 { get; set; }
            public string Column5 { get; set; }
            public string Column6 { get; set; }
        }
    
        static void Main(string[] args)
        {
            var grid = new []
                           {
                               new Row { Column1 = "J6", Column2 = "RES-0112G", Column3 = "123.123", Column4 = "456.456", Column5 = "180", Column6 = "1111"},
                               new Row { Column1 = "FID2", Column2 = "FIDUCIAL", Column3 = "5.123", Column4 = "-50.005", Column5 = "90", Column6 = "FIDUCIAL"},
                               new Row { Column1 = "R100", Column2 = "RES-0113G", Column3 = "1.1", Column4 = "-123.123", Column5 = "90", Column6 = "1111"},
                               new Row { Column1 = "C12", Column2 = "CAP-1234H", Column3 = "-99.99", Column4 = "-987.123", Column5 = "45", Column6 = "2222"},
                               new Row { Column1 = "Q1", Column2 = "CAP-1234Z", Column3 = "-99.99", Column4 = "-987.123", Column5 = "45", Column6 = "4444"},
                               new Row { Column1 = "J3", Column2 = "RES-0112G", Column3 = "123.123", Column4 = "999.999", Column5 = "0", Column6 = "1111"},
                               new Row { Column1 = "FID1", Column2 = "FIDUCIAL", Column3 = "23.123", Column4 = "23.123", Column5 = "0", Column6 = "FIDUCIAL"},
                               new Row { Column1 = "F1", Column2 = "CAP-1234", Column3 = "-88.99", Column4 = "-555.111", Column5 = "45", Column6 = "DDDD"},
                               new Row { Column1 = "C11", Column2 = "CAP-1234C", Column3 = "-123.99", Column4 = "-123.123", Column5 = "270", Column6 = "abc2222"}
                           };
    
            var result = grid.
                GroupBy(r => GetSortValue(r.Column6)).
                OrderBy(g => g.Key, new Column6Comparer()).
                SelectMany(g => g.OrderBy(r => r, new RowComparer()));
    
            foreach (var row in result)
            {
                Console.WriteLine("{0,-6}{1,-13}{2,-10}{3,-12}{4,-6}{5,-10}", row.Column1, row.Column2, row.Column3, row.Column4, row.Column5, row.Column6);
            }
        }
    
        private static string GetSortValue(string source)
        {
            Match match = new Regex(@"[\d]+").Match(source);
            return match.Success ? match.Value : source;
        }
    
        private class Column6Comparer : IComparer<string>
        {
            private Dictionary<string, int> ValueToOrder { get; set; } 
            public Column6Comparer()
            {
                ValueToOrder = new Dictionary<string, int>();
                ValueToOrder["FIDUCIAL"] = 0;
                ValueToOrder["1111"] = 1;
                ValueToOrder["2222"] = 2;
                ValueToOrder["DDDD"] = 3;
                ValueToOrder["4444"] = 4;
            }
    
            public int Compare(string x, string y)
            {
                return ValueToOrder[GetSortValue(x)].CompareTo(ValueToOrder[GetSortValue(y)]);
            }
    
        }
    
        private class RowComparer : IComparer<Row>
        {
            public int Compare(Row x, Row y)
            {
                if (x.Column2 == "FIDUCIAL" && y.Column2 == "FIDUCIAL")
                {
                    return x.Column1.CompareTo(y.Column1);
                }
    
                if (x.Column2 == "FIDUCIAL" || y.Column2 == "FIDUCIAL")
                {
                    return x.Column2 == "FIDUCIAL" ? 0 : 1;
                }
    
                return x.Column2.Substring(4).CompareTo(y.Column2.Substring(4));
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a datagridview with 9 columns. I am simply trying take the value
I have a DataGridView where one of the columns is a DataGridViewComboBoxColumn . When
I have a datagridview with a number of columns, one of these is a
I have a DataGridView set up with the following columns: Teacher, Subject, Date, Period.
I have a datagridview where the users can select which subset of columns to
I have a datagridview made up of multiple rows and columns. I want to
I have a DataGridView that's bound to a DataSet. It has columns DateCreated, Weight,
I have a DataGridView, which is not set to ReadOnly. None of its columns
I've got some Columns in a DataGridView that have their Binding property set to
I have a form that contains dataGridView, whose coloumn are set to dgrv1.Width =dgrv1.Columns.GetColumnsWidth(DataGridViewElementStates.Visible)+20;

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.