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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:44:11+00:00 2026-05-23T14:44:11+00:00

I have a DataGridView in a Winforms application. I want to select a set

  • 0

I have a DataGridView in a Winforms application. I want to select a set of rows in it and sort those rows by a column (Timestamp)…

The rest of the rows should remain as they originally were..
Can this be done using the sort property of DGV

Thanks

  • 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-23T14:44:11+00:00Added an answer on May 23, 2026 at 2:44 pm

    Can this be done using the sort
    property of DGV

    No

    The Sort method on the DataGridView is used for a bit more simple sorting. Such as Ascending or Descending sorting and the SortOrder property is also just for “simple” sorting.

    Can this behavior be implemented? Sure.

    I think the easiest way to do this, is this:

    • Store the index of the first selected item
    • Take out the items that you want to sort from the DataGridView
    • Sort the list using LINQ
    • Append the two lists and add the sorted list at the index stored in the first step.

    However you need to think about how you want to handle if you selecte items that are not followed by each other, for instance if you select index { 1, 3, 6, 9 } you might stil lwant to append this to index 1.

    Edit

    Okay so I played around with this a little bit and came up with a way that you can implement this. It’s not very optimized but you’ll get the idea on how I meant.

    First of all this is my SortSelectedIndices-method that I use:

    static IEnumerable<T> SortSelectedIndices<T>(
        IEnumerable<T> values, 
        IEnumerable<int> selectedIndices, 
        Func<IEnumerable<T>, IEnumerable<T>> sort)
    {
        var selectedValues = new List<T>();
    
        for (var i = 0; i < selectedIndices.Count(); i++)
            selectedValues.Add(values.ElementAt(selectedIndices.ElementAt(i)));
    
        var sortedList = sort(selectedValues);
    
        var finalList = new List<T>();
    
        var startPositionFound = false;
        for(var i = 0; i < values.Count(); i++)
        {
            if (selectedIndices.Contains(i))
            {
                if (startPositionFound) continue;
    
                startPositionFound = true;
                finalList.AddRange(sortedList);
            }
            else
                finalList.Add(values.ElementAt(i));
        }
    
        return finalList;
    }
    

    Then I call it like this:

    static void Main(string[] args)
    {
        var unsorted = new[] {3, 5, 6, 1, 2, 87, 432, 23, 46, 98, 44};
        var selected = new[] {1, 4, 7};
    
        Print(unsorted);
    
        var sort = new Func<IEnumerable<int>, IEnumerable<int>>(
            (x) => x.OrderBy(y => y).ToList());
    
        var sorted = SortSelectedIndices(unsorted, selected, sort);
    
        Print(sorted);
    }
    

    And this prints out the following:

    { 3,5,6,1,2,87,432,23,46,98,44 }
    { 3,2,5,23,6,1,87,432,46,98,44 }
    

    I am just using a simple method here to print this out to the console:

    static void Print<T>(IEnumerable<T> values)
    {
        Console.Write("{ ");
        Console.Write(string.Join(",", values));
        Console.WriteLine(" }");
    }
    

    So what you can do is to have a “sort”-button, when it’s pressed you invoke SortSelectedIndices and then rebind the list when you’re done. Remember I have not profiled or refactored this code, it might not be as fast as you like, I just want to give you an idea on what you can do to acheive the solution.

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

Sidebar

Related Questions

I have a WinForms application with a DataGridView control and a column of DataGridViewButtonCell
i have a winforms application and i want to export data from the datagridview
I have a DataGridView with one DataGridViewComboBoxColumn in my WinForms application. I need to
I have a WinForms app with a datagridview and a bindingsource. I want the
I have a WinForms application with DataGridView control. My control has five columns (say
I have a DataGridView in a Winforms application containing a collection of items. Each
I have a Winforms DataGridView in my application. When the user selects a row
I have a winforms application that I added a DataGridView bound to a subsonic
In a VB.NET Winforms application, I have a form that contains both a datagridview
I have a WinForms application that contains a Dataset ds and a DataGridView dgv.

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.