How to show data in gridview C# .net?

Here you will learn how to show data in gridview?. Simply gridview represent the data in row and column format. To show the data in gridview first you need to insert the data in database and then we will perform gridview operation, for that just follow the below Steps.

Step 1.

Create a New window form application in Microsoft Visual Studio C#.

new form

Step 2.

Now design your window form application like below form.

gridview

Step 3.

Open your Microsoft SQL Server and create a table like as below. My default database is master or you can create your own database if you like.

sql table

Step 4.

Now move to window form application (Form1.cs) and add following namespace in coding window. Simply double click on window form.

	
	using System.Data.SqlClient;
	
namespace

Step 5.

Now create a connection string to connect with Database. Make your Connection globally like below so that you can access it anywhere in the form.

	
	using System;
	using System.Collections.Generic;
	using System.ComponentModel;
	using System.Data;
	using System.Drawing;
	using System.Linq;
	using System.Text;
	using System.Threading.Tasks;
	using System.Windows.Forms;

	using System.Data.SqlClient;

	namespace Intsert
	{
    	public partial class Form1 : Form
    	{
        	public Form1()
        	{
            	InitializeComponent();
        	}

        	//globaly define conection
        	SqlConnection con = new SqlConnection("data source=DESKTOP-F3B3CCJ\SQLEXPRESS; database=master; uid=sa; password=123456;");
        	
    	}
	}
	

You will find your connection details in Sql Server. Open your Microsoft SQL Server.

Go to file menu → Connect Object Explorer… and you will find your connection object.

connection

Step 6.

Now move to window form design view and double click on insert button. Add following code on your insert button click.

	    //insert button coding
        private void button1_Click(object sender, EventArgs e)
        {    
         try
            {        
                con.Open();
                SqlCommand cmd = new SqlCommand("insert into student values ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "')", con);
                cmd.ExecuteNonQuery();
                MessageBox.Show("Data Inserted Successfully.");
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
		

Step 7. Final Step

Now double click on show button and write the below coding.

	
	//show button coding
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("select * from student",con);
                SqlDataAdapter sda = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                dataGridView1.DataSource = dt;
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
	

Output

Now run your application or simply press f5. Now insert the some records in database and then click on insert button. After that click on show button and you will get your data in gridview control.

gridview

Watch Tutorial Video on YouTube

Watch and Subscribe Now

Post Your Comment