Skip to main content

Posts

Showing posts with the label WinForms

How to show excel files inside the .NET Webbrowser Control

If you are reading this, chances are you been banging your head against the wall for a couple of hours (or even days) trying to show excel files inside the WinForms webbrowser control. Possible reasons you ended up in here: You had working code that got broke after upgrading from Win 7. Your code doesn’t work the same way between machines running different (newer) versions of IE. A download box pops up every time your app tries to show an excel file inside the webbrowser control (you wanna show the actual content). You just have no clue on how to get excel working into the .NET embedded webbrowser control. You are trying to implement IInternetSecurityManager and don’t know where to start. (Or how don’t know how to delegate calls to your security manager). Among many other, maybe….. Yes, COM is a PITA, so is ActiveX and IE (Embedded or full for that matter). And no, showing excel files inside the webbrowser control shouldn’t be that hard, but sometimes we have...

Hidden memory leaks in WinForm Apps

Dealing with memory leaks in .NET apps it is always a hard thing to do. While most of the time the GC does a great job freeing us to worry about memory management, there are some edge cases where we have to be very careful to not screw things up. In this post I am going to show a common issue I see when doing code reviews on WinForm apps. First, let’s take a look at this code and see what it is wrong with it. As you can guess, the problem with the code above is that creates a memory leak ;). But why? We are effectively disposing FrmFoo... There are two issues with the following line, The first one, the GC will never be able to collect the garbage created inside the anonymous function generated by the C# compiler to back the lambda expression attached to the control’s click event. Even when we are disposing the form, the object created by the C# compiler is rooted inside the scope of that function and cannot be destroyed; hence the memory leak. ...

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...