How to show a table in dataGridView on the click of button

By yogesh rawat Aug 11 2010
In this blog I will tell you how to show a table on the click of button
    • Like
    • Love It
    • Awesome
    • Interesting
    • It's Okay
    • Thumbs Down
  • 12k
  • 0
First of all you have to create  a table in a database for this you  have to do following things:

1 goto Project then click on Add Components. In Add New Item you have to click Service-based Database .
2. In Server Explorer  you have to add a new table with data by clicking Tables under Data Connection.
3. when you add the table and data in a table you have to write these  code in a button click.

 private void button1_Click(object sender, EventArgs e)       
            SqlConnection sc = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");
            SqlDataAdapter sd = new SqlDataAdapter("select * from customers", sc);
            DataSet ds = new DataSet();
            sd.Fill(ds, "customers");
            dataGridView1.DataSource = ds.Tables[0];

    In this code my table is customers.