WinForm

WinForm

A Windows Forms application is an event-driven application supported by Microsoft's .NET Framework. Unlike a batch program, it spends most of its time simply waiting for the user to do something, such as fill in a text box or click a button.
  • Windows Forms provides access to native Windows User Interface Common Controls by wrapping the existent Windows API in managed code.
  • With the help of Windows Forms, the .NET Framework provides a more comprehensive abstraction above the Win32 API than Visual Basic or MFC did.

Create a WinForm App

The project type comes with all the template files you will need before adding anything. Let's open Visual Studio 2019, if you haven't already installed Visual Studio, go to the Visual Studio downloads page to install it for free.
On the start window, choose Create a new project.
On the Create a new project window, enter or type windows forms in the search box. Next, choose C# from the Language list, and then choose Windows from the Platform list. Select the Windows Forms App (.NET) template, and then choose Next.
In the Configure your new project window, type or enter EFCore5InWinFormApp in the Project name box and click on the Create button, and you will see Visual Studio opens your new project.

Install Entity Framework Core

To use Entity Framework Core we need to install Microsoft.EntityFrameworkCore library. It is available as a nuget package, and you can install it using Nuget Package Manager.
In the Package Manager Console window, enter the following command.
1
PM> Install-Package Microsoft.EntityFrameworkCore
Copied!
For SQL Server LocalDB, which is installed with Visual Studio, we need to install Microsoft.EntityFrameworkCore.SqlServer and will get all the packages required for EF Core.
1
PM> Install-Package Microsoft.EntityFrameworkCore.SqlServer
Copied!

Create a Data Model and Database Context

In Solution Explorer, right-click on your project and choose Add > Class. Enter a class file name Author.cs and add the following code.
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
​
5
namespace EFCore5InWinFormApp
6
{
7
public class Author
8
{
9
public int AuthorId { get; set; }
10
public string FirstName { get; set; }
11
public string LastName { get; set; }
12
public DateTime BirthDate { get; set; }
13
}
14
}
Copied!
So let's create a new class file named BookStore.cs, and replace the following code.
1
using Microsoft.EntityFrameworkCore;
2
using System;
3
using System.Collections.Generic;
4
using System.Text;
5
​
6
namespace EFCore5InWinFormApp
7
{
8
public class AuthorContext : DbContext
9
{
10
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
11
{
12
optionsBuilder.UseSqlServer(@"Data Source=(localdb)\ProjectsV13;Initial Catalog=AuthorDb;");
13
}
14
​
15
public DbSet<Author> Authors { get; set; }
16
}
17
}
Copied!
We need to add DataGridView to display the data from the database, so let's select the DataGridView from the Toolbox and drag it to the Form1 as shown below.
Now in the Form1_Load method, we will add some authors' data to the database first, and then we will retrieve the data and display it on the DataGridView as shown below.
1
private void Form1_Load(object sender, EventArgs e)
2
{
3
using (var context = new AuthorContext())
4
{
5
context.Database.EnsureCreated();
6
var authors = new List<Author>
7
{
8
new Author { FirstName="Carson", LastName="Alexander", BirthDate = DateTime.Parse("1985-09-01")},
9
new Author { FirstName="Meredith", LastName="Alonso", BirthDate = DateTime.Parse("1970-09-01")},
10
new Author { FirstName="Arturo", LastName="Anand", BirthDate = DateTime.Parse("1963-09-01")},
11
new Author { FirstName="Gytis", LastName="Barzdukas", BirthDate = DateTime.Parse("1988-09-01")},
12
new Author { FirstName="Yan", LastName="Li", BirthDate = DateTime.Parse("2000-09-01")},
13
};
14
​
15
context.Authors.AddRange(authors);
16
context.SaveChanges();
17
​
18
dataGridView1.DataSource = authors;
19
}
20
}
Copied!
If you run the application, you will see that authors are successfully inserted into the database and display on the DataGridView.
Last modified 4mo ago