In Focus

Display Data In A DataGridView C# 6

The sample example show you how to use DataGridView to display data in a grid format.

This article is based on the original article that was previously written using an older version of Visual Studio. You can find the original article on the below link:

Step 1: Make a database with table in SQL Server.

1
Figure 1

Step 2: Create a Windows Application and add DataGridView on the Form. Now Add a DataGridView control to the form by selecting it from Toolbox and set properties according to your needs.

2
Figure 2

Adding Source Code for GridView

Now you can add this few lines of code anywhere you want to load the data from the database. It may be a mouse button click or the Form load event handler.
  1. private void Form1_Load(object sender, EventArgs e)  
  2. {  
  3.   SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Student""server = MCNDESKTOP33; 
      database = Avinash; UID = sa; password = *******"
    );  
  4.   DataSet ds = new DataSet();  
  5.   da.Fill(ds, "Student");  
  6.   dataGridView1.DataSource = ds.Tables["Student"].DefaultView;  
  7. }  
Step 3: Code for the Save and Reset button
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10. using System.Data.SqlClient;  
  11.   
  12. namespace WindowsFormsDataGrid  
  13. {  
  14.   
  15. public partial class Form1 : Form  
  16. {  
  17.   
  18. public Form1()  
  19. {  
  20. InitializeComponent();  
  21. }  
  22.   
  23. private void Form1_Load(object sender, EventArgs e)  
  24. {  
  25. SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Student""server = MCNDESKTOP33; 
    database = Avinash; UID = sa; password = *******"
    );  
  26. DataSet ds = new DataSet();  
  27. da.Fill(ds, "Student");  
  28. dataGridView1.DataSource = ds.Tables["Student"].DefaultView;  
  29. }  
  30.   
  31. private void button1_Click(object sender, EventArgs e)  
  32. {  
  33. SqlConnection con = new SqlConnection("server = MCNDESKTOP33; database = Avinash; 
    UID = sa; password = *******"
    );  
  34. con.Open();   
  35. string qur = "INSERT INTO Student VALUES ('" + textBox1.Text + "','" + textBox2.Text + "',
    '"
    +textBox3.Text+"','"+textBox4.Text+"')";  
  36.   
  37. SqlCommand cmd = new SqlCommand(qur, con);  
  38. cmd.ExecuteNonQuery();  
  39. con.Close();  
  40. MessageBox.Show("Inserted sucessfully");  
  41. textBox1.Text = "";  
  42. textBox2.Text = "";  
  43. textBox3.Text = "";  
  44. textBox4.Text = "";  
  45. }  
  46.   
  47. private void button2_Click(object sender, EventArgs e)  
  48. {  
  49. textBox1.Text = "";  
  50. textBox2.Text = "";  
  51. textBox3.Text = "";  
  52. textBox4.Text = "";  
  53.   
  54. }  
  55. }  
  56. }  
Output: The contents of DataGridView look as in the below figure.

Figure 3
3

How to Run

Add using System.Data.SqlClient; namespace in your project.

Change the database path in this string. "server = MCNDESKTOP33; database = Avinash; UID = sa; password = *******"
Run the application.

To read other articles based on the same DataGridView application functionality , please click on the below references link: