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 8170653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:11:43+00:00 2026-06-06T21:11:43+00:00

I have a datagridview that I would like to have rows sorted based on

  • 0

I have a datagridview that I would like to have rows sorted based on the portion of a string entered from a user. The entered string is compared with all of the strings in a particular column. For instance, if I gave “comp” as the search word, the program would try to compare the search word with the strings on first column and sort the rows in a descending order which starts with “comp”, such as “compare”, “composition”, “computer” etc. Rest of the words that do not match is either left alone or sorted in an alphabetical order (whichever is easier).

In LINQ, I am aware that you can apply the following code to achieve what you wanted with a string array:

var sortedWords = words.Where(x => x.Contains("comp"))
                       .OrderByDescending(x => x);

How can I achieve the same thing in Datagridview as I need to have the rows sorted, not just the items inside a particular column?

Edit:

The following code is giving a System.InvalidOperationException. (SetCurrentCellAddressCore is being called twice)

    private void DGVPointCtrl_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        MatchComparer mc = new MatchComparer();
        DGVPointCtrl.Sort(mc); //Error
    }

I’m probably doing something wrong but I’m not sure why. Here is the code that programatically adds the rows for testing purposes:

private void BtnRefresh_Click(object sender, EventArgs e)
        {
            try
            {
                DGVPointCtrl.Rows.Clear();
                int mainIndex = CmbMainDevice.SelectedIndex;
                int subIndex = CmbSubDevice.SelectedIndex;
                DDCDAO ddcdao = new DDCDAO(DDCGlobal.ddcEngineIP, ddc.Ip);
                string pointListType;
                object rs;

                //Currently only supports IO DDC Request
                //TO DO: Change DDCDAO to send proper subdevice requests

                if (mainIndex == 0) //IO
                {
                    #region Main Device: IO
                }


                //First row is for searching items
                DGVPointCtrl.Rows.Add(new DataGridViewRow());
                for (int i = 1; i < 5; i++)
                {
                    DGVPointCtrl.Rows.Add(new DataGridViewRow());
                    DGVPointCtrl.Rows[i].ReadOnly = true;
                }
                DGVPointCtrl.Columns[0].SortMode = DataGridViewColumnSortMode.Programmatic;
                DGVPointCtrl.Rows[0].DefaultCellStyle.Font =
                new Font(DGVPointCtrl.DefaultCellStyle.Font, FontStyle.Italic | FontStyle.Bold);

                if (subIndex == 1) //BI
                {
                    PointDGVColumnGenerate("IO_BI");

                }
                else if (subIndex == 2) //BO
                {
                    PointDGVColumnGenerate("IO_BO");
                }
                else if (subIndex == 3) //AI
                {
                    PointDGVColumnGenerate("IO_AI");
                }
                else if (subIndex == 4) //AO
                {
                    PointDGVColumnGenerate("IO_AO");
                }

                DGVPointCtrl.Rows[1].Cells[0].Value = "IO12314";
                DGVPointCtrl.Rows[2].Cells[0].Value = "IO21948";
                DGVPointCtrl.Rows[3].Cells[0].Value = "IO28194";
                DGVPointCtrl.Rows[4].Cells[0].Value = "VP12984";
                DGVPointCtrl.Rows[2].Cells[1].Value = "asdf";

                #endregion
            }
            catch
            {
            }
      }

    private void PointDGVColumnGenerate(string key)
    {
        int colCount = 0;
        DGVColumnTable.Clear();

        for (int i = 0; i < COL_MAX; i++)
        {
            DGVPointCtrl.Columns[i].HeaderText = "   ";
            DGVPointCtrl.Columns[i].Visible = true;
        }

        foreach (string s in UIConstant.DDCPCtrlListColumnText[key])
        {
            DGVPointCtrl.Columns[colCount].HeaderText = s;
            DGVColumnTable.Add(DGVPointCtrl.Columns[colCount]);
            colCount++;
        }
    }

Edit2:

public class MatchComparer : IComparer
{
    private static IComparer defaultComparer = new CaseInsensitiveComparer();
    int IComparer.Compare(object x, object y)
    {
        DataGridViewRow xr = (DataGridViewRow)x;
        DataGridViewRow yr = (DataGridViewRow)y;

        string xs = "";
        string ys = "";
        try
        {
            xs = xr.Cells[0].Value.ToString();
        }
        catch
        {
        }
        try
        {
            ys = yr.Cells[0].Value.ToString();
        }
        catch
        {
        }

        if (HasMatch(xs) && !HasMatch(ys)) return -1;
        else if (!HasMatch(xs) && HasMatch(ys)) return 1;
        else return defaultComparer.Compare(xs, ys);
    }
  • 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-06-06T21:11:45+00:00Added an answer on June 6, 2026 at 9:11 pm

    This is possible only if you are populating the grid yourself as opposed to binding it to the database.

    Set DataGridViewColumn.SortMode to Programmatic.

    Use DataGridView.Sort to impose a comparer like this:

    public class MatchComparer : IComparer  {
         int IComparer.Compare(object x, object y)  {
             if (HasMatch(x) && !HasMatch(y)) return -1;
             else if (!HasMatch(x) && HasMatch(y)) return 1;
             else return defaultComparer.Compare(x, y);
         }
    
         private bool HasMatch(object x) {
             return x is string && ((string)x).StartsWith("comp");
         }
    
         private static IComparer defaultComparer = new CaseInsensitiveComparer();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a DataGridView that I would like all values on a specific row
I have a datagridview that I have put a ContextMenuStrip1 on. I would like
Hello I have a datagridview with many rows. I would like to be able
I have a DataGridView that gets a datasource assigned. I would like to create
i have a datagridview and i would like the rowheader to correctly select that
I have a Datagridview and would like to change the color of the Rows
I have a datagridview that may or may not have rows selected when the
Ok, I have a DataGridView that I'd like to load with data that I'm
I have a DataGridView in which one column has data that the user needs
I have a DataGridView . I would like to update another cell on the

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.