Home > DeveloperSection > Forums > DataGridView Binding - SQL Server dataset in C#
Takeshi Okada
Takeshi Okada

Total Post:89

Points:629
Posted on    June-15-2013 4:46 AM

 C# C# 
Ratings:


 1 Reply(s)
 1078  View(s)
Rate this:
hi Developers,

How to bind the DataGridView with SQL Server dataset in C#.


Sumit Kesarwani

Total Post:378

Points:2694
Posted on    June-15-2013 7:42 AM

Hi Takeshi,

You can use this code:

using System;

using System.Data;

using System.Windows.Forms;

using System.Data.SqlClient;

 

namespace WindowsApplication1

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            string connectionString = "Data Source=.;Initial Catalog=pubs;Integrated Security=True";

            string sql = "SELECT * FROM Authors";

            SqlConnection connection = new SqlConnection(connectionString);

            SqlDataAdapter dataadapter = new SqlDataAdapter(sql, connection);

            DataSet ds = new DataSet();

            connection.Open();

            dataadapter.Fill(ds, "Authors_table");

            connection.Close();

            dataGridView1.DataSource = ds;

            dataGridView1.DataMember = "Authors_table";

        }

    }

}


Don't want to miss updates? Please click the below button!

Follow MindStick