C#: How to Bind data to DataGridView in Windows Forms Application

Irit Kushwaha
GDSC UMIT
Published in
2 min readSep 27, 2021

In this tutorial, I would like to share some of my experience in how to bind (fill) DataGridView control in Windows Forms (WinForms) Application using DataTable in C#.

Getting started 🚀

First, we need to create a new console application project “Database Creation” in Visual Studio, and choose Language “C#”. Then create a database of “employees” and inside that database create a table “ telephone”.

Add the following columns to your telephone table 📌

  1. Sr.No
  2. officer_name
  3. designation
  4. dept
  5. ext_no
  6. std_no
  7. mobile_no
  8. room_no
  9. address
  10. senior_no.

Insert the data into your respective table.

🟣 Adding a DataGridView to the Windows Form

Firstly you need to add a DataGridView control to the Windows Form from the Visual Studio ToolBox.

The following HTML consists of an ASP.Net GridView with BoundField columns and they have been sorted according to seniority number i.e senior_no. You can add a search function also so that anyone can search employees by their name, designation, and department.

🟣 Populating GridView from database

Okay, So now it’s time to create a Code File for the above HTML file.

Namespaces

You will need to import the following namespace.

Below is the simple and straightforward way of binding data to DataGridView control using DataTable.

In the below code, the DataTable is populated from the telephone Table of employees Database, and then it is set as DataSource to the DataGridView control.

In this approach, all columns returned by the Select Query will be displayed in the DataGridView.

This is how it looks like 🔥

Conclusion 🤞

That is it as far as basics go when it comes to using Grid View. There is obviously so much more to know and I would definitely encourage you to build a few small projects and experiment 💪

If you have any questions about this tutorial, please let me know in the comments section of this blog post. You can also contact me through Linkedin. I’m happy to assist you 😊!

--

--