How to Create a Metro GridView in C#

By FoxLearn 7/22/2024 2:13:59 AM   27.12K
To use Metro GridView with Metro Framework in a C# Windows Forms application, and then retrieve data from a SQL database using Entity Framework, follow these steps.

Metro Framework is an open source library using c# ui framework that helps you design a modern awesome ui.

How to use Metro GridView in C#

Open your Visual Studio, then create a new Windows Forms Application project.

Next, right click on your project and select Manage NuGet Packages -> Search 'metro framework' -> Install

If you don't see the metro framework in your Visual Studio toolbox, you can view How to download and install metro framework

Drag and drop MetroLabel, MetroTextbox, MetroGridView and MetroButton controls from the visual studio toolbox to your window form, then create a simple metroframework modern ui sample project as shown below.

c# metro gridview

We will use Entity Framework to fetch data from sql database, so you need to right click on your project select Add -> New Item -> ADO.NET Entity Data Model -> Select Northwind database -> Products table.

If you haven't got Northwind database, you can view How to download and restore Northwind database in SQL Server

To create metro or modern ui style windows application in c#. You need to change the inheritance from the Form to the MertroForm, then add code to initialize your MetroStyle as shown below allows you to apply metro framework change theme.

public Form1()
{
    InitializeComponent();
    //Init theme, style
    this.StyleManager = metroStyleManager1;
    metroStyleManager1.Theme = MetroFramework.MetroThemeStyle.Light;
    metroStyleManager1.Style = MetroFramework.MetroColorStyle.Green;
}

Add the click event handler to the Load button allows you to retrieve data from the sql database. You should add the BindingSource control to the MetroGridView and TextBox control, then set the DataSource for BindingSource control as the following c# code.

private void btnLoad_Click(object sender, EventArgs e)
{
    using (NorthwindEntities db = new NorthwindEntities())//Create an instance for data context
    {
        productBindingSource.DataSource = db.Products.ToList();
    }
}

VIDEO TUTORIAL