Bind DataGridview with Database

This blog defines the code to bind DataGridview with database.

public void displayDataGridView()
{
    SqlConnection con = new SqlConnection("server = MUNESH;Database=datastore;UID=sa;Password=123;");
    SqlCommand cmd = new SqlCommand("Select * from data1", con);
    try
   {
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = cmd;
        DataTable dt = new DataTable();
        da.Fill(dt);
        dataGridView1.DataSource = dt;
        con.Close();
    }
    catch (Exception ec)
   {
        MessageBox.Show(ec.Message);
   }
}

Ebook Download
View all
Learn
View all