Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to update datagridview?

I use below code to update datagridview in my application with Timmer. Timmer runs in everysecond and it keeps the screen flashing. How can i change not to flash at the time? or another way to update datagridview?

        SqlConnection mySqlConnection = new SqlConnection(SQLCONN);
        mySqlConnection.Open();

        SqlDataAdapter addapter = new SqlDataAdapter();
        DataTable dt = new DataTable("SSReportAmalgamate");
        SqlCommand cmd = mySqlConnection.CreateCommand();

        cmd.CommandText = "EXEC App_GetDATA " + "@acc" + "," + "@selecttype";
        cmd.Parameters.Add("@acc", SqlDbType.Char).Value = acc;
        cmd.Parameters.Add("@selecttype", SqlDbType.Char).Value = type;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandType = CommandType.Text;
        cmd.Connection = mySqlConnection;
        addapter.SelectCommand = cmd;

        addapter.Fill(dt);
        dataGridView1.DataSource = dt;

        mySqlConnection.Close();
like image 604
Jay Avatar asked Feb 01 '26 22:02

Jay


1 Answers

The problem is, that you update the form even the data has not been changed. Instead, try to hear to an event from the SQL Server e.g. using a SqlDependency when there is new data (of course, if your data changes not often).

like image 143
alex555 Avatar answered Feb 03 '26 13:02

alex555