DataGridView Control in Visual Studio

Introduction

The DataGridView Control displays data in a tabular format. A DataGridView Control provides a user interface for ADO.NET datasets. The DataGridView Control displays data by making a connection between C# and the SQL Server. The DataGridView Control is the best useful and flexible control in the Windows Forms Application. The DataGridView Control can be useful to display either a one table or relationships between a combination of tables.

Getting Started of Working

We first open Visual Studio 2013 as an administrator and create a New Windows Forms Application. Create it using File -> New -> Project and click on the OK button.


Now we need to drag and drop the DataGridView control from the Toolbox to the Windows Form.


To fetch the data for the DataGridView Control in Visual Studio we first need to create a table in the SQL Server. Now we create a table in the SQL Server.

To create the table we first need to create a database.

  1. create database database_name   
For using the database we need the following query to execute.   
  1. use database_name 
Now create a table using the following:

  1. CREATE TABLE EMP   
  2. (  
  3.    EMPNO NUMERIC(4) NOT NULL,  
  4.    ENAME VARCHAR(10),  
  5.    JOB VARCHAR(9),  
  6.    MGR NUMERIC(4),  
  7.    HIREDATE DATETIME,  
  8.    SAL NUMERIC(7, 2),  
  9.    COMM NUMERIC(7, 2),  
  10.    DEPTNO NUMERIC(2)  
  11. )  
Then press the F5 key to create the table.

We will now insert some data into the EMP table using the following:

 

  1. INSERT INTO EMP VALUES(7369, 'YATENDRA','ENGINEER',7902,'17-DEC-1980',20000, NULL, 20)  
  2. INSERT INTO EMP VALUES(7499, 'ALLEN''SALESMAN', 7698, '20-FEB-1981', 1600, 300, 30)  
  3. INSERT INTO EMP VALUES(7521, 'WARD''SALESMAN', 7698,'22-FEB-1981', 1250, 500, 30)  
  4. INSERT INTO EMP VALUES(7566, 'JONES''MANAGER', 7839, '2-APR-1981', 2975, NULL, 20)  
  5. INSERT INTO EMP VALUES(7654, 'MARTIN''SALESMAN', 7698, '28-SEP-1981', 1250, 1400, 30)  
  6. INSERT INTO EMP VALUES(7698, 'BLAKE''MANAGER', 7839, '1-MAY-1981', 2850, NULL, 30)  
  7. INSERT INTO EMP VALUES(7782, 'CLARK''MANAGER', 7839, '9-JUN-1981', 2450, NULL, 10)  
  8. INSERT INTO EMP VALUES(7788, 'SCOTT''ANALYST', 7566, '09-DEC-1982', 3000, NULL, 20)  
  9. INSERT INTO EMP VALUES(7839, 'KING''PRESIDENT'NULL'17-NOV-1981', 5000, NULL, 10)  
  10. INSERT INTO EMP VALUES(7844, 'TURNER''SALESMAN', 7698, '8-SEP-1981', 1500, 0, 30)  
  11. INSERT INTO EMP VALUES(7876, 'ADAMS''CLERK', 7788, '12-JAN-1983', 1100, NULL, 20)  
  12. INSERT INTO EMP VALUES(7900, 'JAMES''CLERK', 7698, '3-DEC-1981', 950, NULL, 30)  
  13. INSERT INTO EMP VALUES(7902, 'FORD''ANALYST', 7566, '3-DEC-1981', 3000, NULL, 20)  
  14. INSERT INTO EMP VALUES(7934, 'MILLER''CLERK', 7782, '23-JAN-1982', 1300, NULL, 10)  

Now back to the Windows Forms Application. After creating the table and inserting the data into the EMP Table we will now choose the Data Source from the drop down list.


Click on the Add Project Data Source.


Now choose the Data Source type, where we will store the data and select the Database from the data source type.


Click on the next Button, now choose Database Model and click on the next Button.


Now choose your Data Connection, click on the New Connection.


Select Server Name.


Select a database or enter a database name.


Choose your Database Objects and select the EMP table from the list, select all the fields of the table in the Data Source Configuration Wizard.


After selection click on the Finish Button.

Now the Data Source has been created. Now we need to right-click on the DataGridView Control and from the options we select the Data Source, select the Data Source we have created.


Before running the application our form looks like this.


Now we need to build and run the application. After running the application the Output window will look as in the following and all the records will be fetched that we inserted into the SQL Server database.


I hope this article is helpful for the readers that want to work with the DataGridView Control in Visual Studio 2013 without writing any code in Windows Forms Applications.

Thanks for reading this article, I hope you guys like it.


Similar Articles