How to suspend a DataGridView while updating its columns

前端 未结 6 1570
无人及你
无人及你 2020-12-09 11:19

How can I suspend a .NET DataGridView from displaying anything while I update its Columns?

Here\'s my current code. It works ok, but it is very slow on the foreach

相关标签:
6条回答
  • 2020-12-09 11:25

    In my case suspend and resume layout did not work. I resolved disabling the dataGridView (dgv.Enabled = false) before update and re-enabling it (dgv.Enabled = true) at the end of the update process.

    0 讨论(0)
  • 2020-12-09 11:36

    You could try and prevent it from completely redrawing by using the code in this post. The parent would be the parent of the dataGridView1.

    0 讨论(0)
  • 2020-12-09 11:36

    Significant performance increase:

    var dgv = new DataGridView();
    dgv.SuspendLayout();
    // Do update, change values
    dgv.ResumeLayout();
    

    May not be ultimate high performance.

    0 讨论(0)
  • 2020-12-09 11:41

    You may want to consider using the AddRange method instead of Add. The Data Grid behaves a little better when you add them all at once.

    DataGridViewColumn[] columns = new DataGridViewColumn[dt.Columns.Count];
    
    for (int i = 0; i < dt.Columns.Count; i++ )
    {
        DataColumn c = dt.Columns[i];
        DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
        col.SortMode = DataGridViewColumnSortMode.NotSortable;
        col.DataPropertyName = c.ColumnName;
        col.HeaderText = c.Caption;
    
        columns[i] = col;
    }
    
    
    dataGridView1.Columns.AddRange(columns);
    
    0 讨论(0)
  • 2020-12-09 11:41

    If you are using timer then use SynchronizingObject. This removes the flickering completly for me.

    var dgv = new DataGridView();
    System.Timers.Timer timer = new System.Timers.Timer();
    timer.Interval = 1000;
    timer.SynchronizingObject = dgv;  // syncronise
    timer.Start();
    timer.Elapsed += Timer_Elapsed;
    void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        refreshDGV();  // in here I refresh the DataGridView
    }
    
    0 讨论(0)
  • 2020-12-09 11:52

    You can use VirtualMode with DataGridView in order to very efficiently update the grid. See this article: http://msdn.microsoft.com/en-us/library/ms171622.aspx

    From what I remember, it seems to update the entire collection before updating anything on the UI, as opposed to adding to the UI for each new row added/etc.

    0 讨论(0)
提交回复
热议问题