[C#] Refresh DataGridView using DataSet and

In order to “update” / “refresh” the DataGridView (DGV in brief), I search the whole Google and finally, I figure out this good solution.
My condition is that in case you have a DGV, you want to insert, update, delete some record and instantly refresh the DGV result/content.
Here, you will need a DataSet and with the following code,

public partial class Form1 : Form
{
        #region Form1
        private SqlDataAdapter SDA = new SqlDataAdapter();
        #endregion

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection();
            SqlCommand cmmd = new SqlCommand();
            DataSet ds = new DataSet(); //
            SqlDataReader dr;
            conn.ConnectionString = "Data Source=***;Initial Catalog=***;Persist Security Info=True;User ID=***;Password=***";
            conn.Open();
            cmmd.Connection = conn;

            cmmd.CommandText = "select * from table";

            SDA.SelectCommand = cmmd;

            SDA.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0].DefaultView;

        //...
        }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s