C# program create a windows form to show stored data in sql server database on form using GridView control

Category > CSHARP || Published on : Friday, November 20, 2020 || Views: 1006 || create a windows form to show stored data in sql server database on form using GridView control


Here Pawan Kumar will explain how to create a windows form to show stored data in sql server database on form using GridView control

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

namespace WindowsFormsshowdataGridview
{
    public partial class Form1 : Form
    {
        SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=DbName;Integrated Security=True;Pooling=False");
        SqlDataAdapter da = new SqlDataAdapter();
        DataSet ds = new DataSet();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ds.Clear();
            string str = "select * from Student";
            da = new SqlDataAdapter(str, con);
            da.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];

        }
    }
}