Skip to main content

Posts

Showing posts with the label DataGridView

WinForms, How to enable sorting for a datagridview that uses linq queries as data source

How to enable sorting for a datagridview that uses linq queries as data source it is a frequently asked question on the web. The short answer is you cannot, but let dig a little deeper. Why the sorting functionality works as expected when you use it on a grid binded to a DataTable and miserably fails when you bind it to an IEnumerable<DataRow>?  A bit of research later… Easy, the DataTable class provides its own implementation for sorting rows whereas IEnumrable<T> does not. I faced this problem building the power grid control, because (to make paging records easy) internally I was using linq queries. As soon as I realized that the datagrid sorting was broken, I started to look on the web an easy way to solve this problem (other than implement my own sorting mechanisms, of course). I found a couple of alternatives but this one  convinced me. This class has a neat way to solve generic sorting with a minimal impact on performance. I’ve tested it sorti...

WinForms, how to implement conditional format for DataGridViews (part 2)

In the previous post I showed you how to implement conditional format functionality for datagridviews using C#, working on conditions that we already know at compile time, which means that those conditions will be written by programmers, not end users. In this post I’ll show you how to deal with user defined conditional formats, which is a more useful thing to do if what you want to provide this kind of functionality. With this new feature the users are going to be able to write conditions in an Excel fashion (formula like, if you will) To implement this feature I’ve upgraded the project that we been using for this series to .NET Framework 4.5 in order to use the latest version of the Roslyn compiler. The Roslyn compiler is the backbone of this feature. If you are interested in Roslyn, I made a series on How to build a DSL on top of Roslyn  that covers some basic stuff of this great piece of software. As in the Roslyn DSL series I'll rely on source to source translati...

WinForms, how to implement conditional format for DataGridViews

Being able to apply conditional formats to datagridviews is a must nowadays, users of our app probably make heavy use of this feature on Excel spreadsheets, and they want it to be present on any "grid alike" screen. I’ll show you two ways to implement this feature the first one is using pre-built formats, or formats specified in C# at compile time (better performance) and  the second one that allows users to add their own conditional formats with formulas written in an excel fashion . In the last case the performance is not good enough for prod., but I’ll be sanding rough edges and hopefully make it work in a near future. To work on this feature, I'm going to use the same datagrid I used in the previous post, which you can download from here . The whole functionality is already implemented, so I'll just show you how to use it. The only thing you have to do is register the conditional format and the component will take care of the rest.  To register a...

WinForms, paging the DataGridView the right way

I know this may sound like old history, but in the enterprise world there is still a lot of WinForms development. Just a couple of days ago, I had to implement a custom DataGridView capable to work over a butt load of data (100K+ records) and keep responses times acceptables. I thought paging will be a good way to go, and as WinForms is pretty old nowadays, I supposed it will be easy to find a couple examples on the web. While in fact I found examples, all of them were incompletes and/or they wouldn't perform well in real world apps... So I decided to roll my own component and post it online. Hopefully, someone else will find it useful ;). The bread and butter of this solution relies on LINQ and deferred execution. As LINQ takes care of all complicated work, it was quite easy to implement. This component also supports conditional format, sorting and some search capabilities, but in this post I will concentrate on paging only (I'll cover the rest of the features in f...