The .Net IComparer Interface - Part 1

______________________

Desktop / Microsoft Windows Forms application will probably need a way to sort the data presented to the user. If that application presents the data in a DataGridView, sorting can become quite a challenge. This article describes a solution built around the IComparer Interface, focusing on sample C# and VB.net Windows Forms applications. Note that ASP.net also handles the IComparer Interface.

Part one here will describe the application user experience.

Part two will describe the main form engineering of the C# solution.

Part three will describe the C# solution IComparer sort machinery.

Part four will explain the C# solution data display form, and wrap up with a brief description of the differences between the C# and VB.net example applications.

Download the sample C# / VB.net software at the GitHub repository.

________________

I recently built a VB.net Windows Forms application - .NET Framework 4.5 - to front-end a SQL Server 2012 database. The application presents the database result sets in a DataGridView form object. In the application, the user can pick one or more DataGridView rows and click a button that sends data from the picked rows to MS Word. The application, including the DataGridView, preserves the sort order of the original SQL Server stored procedure result set.
 Microsoft explains that "[t]he DataGridView control provides a customizable table for displaying data", with almost infinite flexibility through its huge number of properties, events, and controls. Unfortunately, the DataGridView does not offer native sort order control for selected rows. In the application, this matters because the user needs full ascending / descending sort control, and complete sort column ordering flexibility, for all selected DataGridView rows the application passes to MS Word. MS Word, and by extension Word VBA, can handle all this but the Word VBA object model can become nasty. I tried LINQ, but I never found a way to make this approach work. The IComparer Interface solved the problem. After I finished the first application, I built a second sample application in C# and VB.net to highlight the technique. Part one here will first focus on the sample application user experience - identical for both versions.
________________

The first form


1. The first form
opens with a fourteen row, eight column row set in a DataGridView, highlighting the first row. The user can pick zero or more rows


2. Highlighting DataGridView rows in the first form
and when the user clicks "Load RTB"


3. Second form - DataGridView columns Recipe Name
and Recipe Text of selected rows in first
form placed in a RichTextBox
form textForm1 opens. This form only shows the
RECIPE NAME
and
RECIPE TEXT
column values from the selected DataGridView rows in a Rich Text Box, although we can easily add, remove, and / or change column values in the application code-behind. The Rich Text Box shows these record values separated by
____________
________________________
____________

strings. The application sorted the recipes by the
CATEGORY (ascending)
and then by the
ROW (descending)
columns from DataGridView of the first form. As we'll see, however, we can easily change the sort criteria, again in the application code-behind. To close the second form and return to the first form, the user clicks the Close button. At the first form, the user clicks the Close button to close the application.
_______________

Part 1 here showed how the sample application works. Part 2 will describe the C# application engineering.