DATAGRIDVIEW BINDING - SQL SERVER DATASET IN C#

Takeshi Okada

Total Post:89

Points:629
Posted by  Takeshi Okada
C# 
C#
 1407  View(s)
Ratings:
Rate this:
hi Developers,

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

    Post:378

    Points:2694
    Re: DataGridView Binding - SQL Server dataset in C#

    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";

            }

        }

    }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!