0

I'm populate a datagridview from sql server. Also i have create a class for using double buffer property.

ExtensionMethods

 public static class ExtensionMethods
 { 
    public static void DoubleBuffered(this DataGridView dgv, bool setting)
    {
        Type dgvType = dgv.GetType();
        PropertyInfo pi = dgvType.GetProperty("DoubleBuffered",
            BindingFlags.Instance | BindingFlags.NonPublic);
        pi.SetValue(dgv, setting, null);
    }
  }

And inside form in load event

 private void CustomerTrans_Load(object sender, EventArgs e)
    {
        CustomersGrid.DoubleBuffered(true);
      
        try
        {
            using (SqlConnection connection = new SqlConnection(Connection.connectionString))
            using (SqlDataAdapter sda = new SqlDataAdapter("select TrnDocumentID,CustomerID,CustomerName,DateTime,Datetime,TrnName,SumQuantity,SumTotal,SumDiscountTotal,TableName,UserName,UserID from CustomerTrans  where DateTime >= '" + DateTime.Now.ToString("M/d/yyyy") + "'", connection))
            {
                DataTable dt = new DataTable();
                sda.Fill(dt);
                CustomersGrid.Rows.Clear();
                foreach (DataRow item in dt.Rows)
                {
                    int n = CustomersGrid.Rows.Add();
                    CustomersGrid.Rows[n].Cells[0].Value = item[0].ToString();
                    CustomersGrid.Rows[n].Cells[1].Value = item[1].ToString();
                    CustomersGrid.Rows[n].Cells[2].Value = item[2].ToString();
                    CustomersGrid.Rows[n].Cells[3].Value = Convert.ToDateTime(item[3]).ToString("dd/MM/yyyy");
                    CustomersGrid.Rows[n].Cells[4].Value = Convert.ToDateTime(item[4]).ToString("HH:mm");
                    CustomersGrid.Rows[n].Cells[5].Value = item[5].ToString();
                    CustomersGrid.Rows[n].Cells[6].Value = Convert.ToDecimal(item[6].ToString());
                    CustomersGrid.Rows[n].Cells[7].Value = Convert.ToDecimal(item[7].ToString());
                    CustomersGrid.Rows[n].Cells[8].Value = Convert.ToDecimal(item[8].ToString());
                    CustomersGrid.Rows[n].Cells[9].Value = item[9].ToString();
                    CustomersGrid.Rows[n].Cells[10].Value = item[10].ToString();
                    CustomersGrid.Rows[n].Cells[11].Value = item[11].ToString();
                }
            }

        }
        catch (Exception ex)
        {

        }

I dont know the reason but the performance is still very slow when i'm scrolling datagridview. Is there anything else which i missed to do?

Community
  • 1
  • 1
Dim
  • 1
  • 3
  • looks ok. How many rows are there typically? – TaW Aug 26 '18 at 15:04
  • i have 20197 rows – Dim Aug 26 '18 at 15:05
  • Hm, while this is a lot scrolling should still be ok.. [2000 were just fine](https://stackoverflow.com/questions/41893708/how-to-prevent-datagridview-from-flickering-when-scrolling-horizontally/41894210#41894210) – TaW Aug 26 '18 at 15:06
  • DoubleBuffer wont make any difference with that code because the form is shown *after* the Load event. That code also has little to do with scrolling, maybe there is something in an event (?). The code could certainly be more performant using databinding. – Ňɏssa Pøngjǣrdenlarp Aug 26 '18 at 15:26

0 Answers0