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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:42:47+00:00 2026-06-09T03:42:47+00:00

How to freeze first row in Datagridview when using a custom SortComparer? Without SortComparer

  • 0

How to freeze first row in Datagridview when using a custom SortComparer?
Without SortComparer row[x].froozen = true; would do it.
But with a SortComparer it doesnt work

Here is my SortComparer Code:

DataGridView dg = (DataGridView)sender;

        if (e.Column.Index == 0)
        {
            e.SortResult = System.String.Compare(e.CellValue1.ToString(), e.CellValue2.ToString());

            if (e.SortResult == 0)
            {
                e.SortResult = System.String.Compare(
                    dg.Rows[e.RowIndex1].Cells[1].Value.ToString(),
                    dg.Rows[e.RowIndex2].Cells[1].Value.ToString());
            }

            e.Handled = true;
        }
        else if (e.Column.Index == 1)
        {

            e.SortResult = System.String.Compare(e.CellValue1.ToString(), e.CellValue2.ToString());

            if (e.SortResult == 0)
            {
                e.SortResult = System.String.Compare(
                    dg.Rows[e.RowIndex1].Cells[0].Value.ToString(),
                    dg.Rows[e.RowIndex2].Cells[0].Value.ToString());
            }
            e.Handled = true;
        }
  • 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-09T03:42:49+00:00Added an answer on June 9, 2026 at 3:42 am

    You could extend your SortComparer to check if the first row index is the top row or not. If it is, set the e.SortResult = 0 or else run whatever code you want to sort with.

      private void dataGridView1_SortCompare(object sender,  DataGridViewSortCompareEventArgs e)
      {
           DataGridView dg = (DataGridView)sender;
           if (e.RowIndex1 == 0) {
               e.SortResult = 0;
               e.Handled = true;
           } else {
             // rest of your comparison code
           }
      }
    

    or you can make it more fancy by checking if the row is frozen or not:

    EDIT the following does not work since the Frozen property of a Row gets set to false by default for some reason

      private void dataGridView1_SortCompare(object sender,  DataGridViewSortCompareEventArgs e)
      {
           DataGridView dg = (DataGridView)sender;
           if (dg.Rows[e.RowIndex1].Frozen) {
               e.SortResult = -1;
               e.Handled = true;
           } else {
             // rest of your comparison code
           }
      }
    

    EDIT The e.SortResult = 0 instead of -1 as rows that are equal will be sorted based on their current position.

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

Sidebar

Related Questions

Using MonoTouch.Dialog is it possible to freeze the first/top row (like a table header)?
freeze the first two column but if I want to freeze the column dynamically
I need to freeze the first column and first row of data in a
I'm using cx_Freeze to freeze my Python code so I can distribute it as
I'm trying to build my first project with the andengine. things work fine so
Possible Duplicate: How can I lock the first row and first column of a
I generate an excel spread-cheat with c# and I want to freeze the first
First of all, here's my config: VS2010/Debug/C++ Win32 Project/Vista Home Premium Invoking GetOpenFileName via
I want to freeze first 2 columns in my WPF Datagrid which I managed
When i use loader.load(request); for the first time, my flex freeze for 10 secondes

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.