Friday, June 19, 2020

There is something wrong with the .NET developers (when it comes to display data on a form)

Note to self: Caption all the caps!

There is something wrong with the .NET developers. There are so many stupid examples flying around when it comes to data binding, data editing and basic CRUD operations. Its just plain wrong.

I’m coming from Delphi background and you cannot even guess how comfortable it is to write database applications within the Delphi IDE. Just put a datagrid, a connection component, a dataset component and a datasource component onto a form. Set grid’s datasource property to the datasource component, set the connection. All you need to write is one line of SQL code. For example “Select * from Products”. You will have products table showing in the datagrid on your form. Yes, you don’t even have to run the application to see the data.


Yes, you’ve heard it right. You don’t have to write code to get the data from server, display it on a grid, edit the data and then post it. Or add/insert new records. All you have to write is a line of SQL code.

We know that the Delphi is the brain child of a genius called Anders Heilsberg. Yes, the same guy who invented C# for the Microsoft. I see examples on the net using C# to retrieve data, edit it, add new records and delete records. All by hand! There are things called data bindings, Entity Framework etc to use data with minimum effort just like we do in Delphi. I’ve seen people using select queries to retrieve the data and then when they edit it they write update queries, they write delete queries… What’s wrong with you people! You don’t have to manually do these tasks. Anders Heilsberg has done it for you already. People are assigning values of text boxes to parameters, getting the data from cells of their DataGridView. You have data in your hands but you are using grid cells… C’mon.

I decided to provide proper examples to people who are learning the C# and database programming. I am also learning the language and its data bound operations for some time. It would be helpful to me when I need to remember the basics. I am tired of inventing the wheel every time I’m in need to display some data on a form.

I will show you a basic list form, a form with editing capabilities, a form with master detail editing functions. All of the necessary CRUD operations without writing much code. There are ways to create forms with data bound components on them with zero coding but it is not like Delphi and you might not have full control over the data. For example, you have to put a save button on a navigator component and then write “db.SaveChanges()” to its OnClick event to save the changes. So, I’m not going to evaluate the “zero code” version. I will implement a few basic CRUD scenarios.

What I’m planning here is to create a basic list form to select some of the records and retrieve the list of selected records to the caller. Not a big deal.



First things first. Let’s create a new WinForms project for this.

I am using Visual Studio 2019 Community Edition for doing the examples. Start Visual Studio and select Windows Forms App from Create New Project form.




 I renamed my app.


Get some packages

Nowadays I'm using Entity Framework Core, but let's go classic (since Core has no proper WinForms support)

Add Entity DataModel

We're ready. Now create a form.

Create a DB connection and a BindingSource.

Show (and automatically dispose) the look up form.

Easy peasy. However this is the default "automatically create colums" grid. Let's put up some make up.

For example, I would like to show "Grup ID" instead of the field name at the header.

Nice!

Let's put the rest.

Here is an example


You don't have to write code for this. This is where I like Delphi much. But don't worry, there is a way for this in our beloved Visual Studio.


The most important thing here is the DataPropertyName which requires the field name (or the property  name of the field in our model class)

We can comment out the make up code now since we fixed it on the form using property editors of DataGridView.

Let't get a navigation bar. I wrote a perfect navigation bar for my Delphi projects and I think I've been using it for more than 18 years.

You can customize it.

Here is what I miss from Delphi again. I added a save changes button yet it does not save the changes. 

We do it by a line of C# code.

Let's put a search text box.

Fix the anchors of the grid to bottom (the important part)

And fix both bottom and top anchors for the text box.

This way our form will adapt to size changes. No code, no effort.

Write some code for our text box on change event so that we can filter the records.

Filter code is a simple lambda expression.

Yet it is powerful.

Here is all the code for our working form. Not much, not less.

Lets add a detail grid (here is the reason why we did not fix the right sides of the grids)

We will create a master/detail relation. Note that I have never wrote code for this in Delphi.

Add a company binding source to our code.

Whenever we click another line on the group grid or press on the navigator buttons a "current item changed" event occurs. I've seen C# coders in the wild who hooks the grid events to do this. You have data in your hands. Why bother with the presentation of it?

We can write another filter for the details grid (company DataGridView)

Voila! It is working!

No comments:

Post a Comment

A Survey of Body Area Networks