<p>In my application I have a DataGridView control that displays data for the selected object. When I select a different object (in a combobox above), I need to update the grid. Unfortunately different objects have completely different data, even different columns, so I need to clear all the existing data and columns, create new columns and add all the rows. When this is done, the whole control flickers horribly and it takes ages. Is there a generic way to get the control in an update state so it doesn't repaint itself, and then repaint it after I finish all the updates? </p>

<p>It is certainly possible with TreeViews:</p>

<pre><code>myTreeView.BeginUpdate();
try
{
    //do the updates
}
finally
{
    myTreeView.EndUpdate();
}
</code></pre>

<p>Is there a generic way to do this with other controls, DataGridView in particular?</p>

<p>UPDATE: Sorry, I am not sure I was clear enough. I see the "flickering", because after single edit the control gets repainted on the screen, so you can see the scroll bar shrinking, etc.</p>

[{'answer_id': 63986, 'author': 'TheSmurf', 'author_id': 1975282, 'author_profile': 'https://Stackoverflow.com/users/1975282', 'pm_score': 1, 'selected': False, 'text': '<p>Sounds like you want double-buffering:</p>\n\n<p><a href="http://www.codeproject.com/KB/graphics/DoubleBuffering.aspx" rel="nofollow noreferrer">http://www.codeproject.com/KB/graphics/DoubleBuffering.aspx</a></p>\n\n<p>Although this is mainly used for individual controls, you can implement this in your Windows Forms control or Form.</p>\n'}, {'answer_id': 64007, 'author': 'TK.', 'author_id': 1816, 'author_profile': 'https://Stackoverflow.com/users/1816', 'pm_score': 1, 'selected': False, 'text': '<p>Unfortunatly, I think that thins might just be a by-product of the .net framework. I am experiencing similar flickering albeit with custom controls. Many of the reference material I have read indicates this, alongside the fact the the double buffering method failed to remove any flickering for me.</p>\n'}, {'answer_id': 64182, 'author': 'Skizz', 'author_id': 1898, 'author_profile': 'https://Stackoverflow.com/users/1898', 'pm_score': 3, 'selected': False, 'text': "<p>Double buffering won't help here since that only double buffers paint operations, the flickering the OP is seeing is the result of multiple paint operations:</p>\n\n<ul>\n<li>Clear control contents -> repaint</li>\n<li>Clear columns -> repaint</li>\n<li>Populate new columns -> repaint</li>\n<li>Add rows -> repaint</li>\n</ul>\n\n<p>so that's four repaints to update the control, hence the flicker. Unfortunately, not all the standard controls have the BeginUpdate/EndUpdate which would remove all the repaint calls until the EndUpdate is called. Here's what you can do:</p>\n\n<ol>\n<li>Have a different control for each data set and Show/Hide the controls,</li>\n<li>Remove the control from its parent, update and then add the control again,</li>\n<li>Write your own control.</li>\n</ol>\n\n<p>Options 1 and 2 would still flicker a bit.</p>\n\n<p>On the .Net GUI program I'm working on, I created a set of custom controls that eliminated all flicker. </p>\n"}, {'answer_id': 65578, 'author': 'Ken Wootton', 'author_id': 7357, 'author_profile': 'https://Stackoverflow.com/users/7357', 'pm_score': 3, 'selected': False, 'text': '<p>The .NET control supports the <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.control.suspendlayout.aspx" rel="noreferrer">SuspendLayout</a> and <a href="http://msdn.microsoft.com/en-us/library/y53zat12.aspx" rel="noreferrer">ResumeLayout</a> methods.  Pick the appropriate parent control (i.e. the control that hosts the controls you want to populate) and do something like the following:</p>\n\n<pre><code>this.SuspendLayout();\n\n// Do something interesting.\n\nthis.ResumeLayout();\n</code></pre>\n'}, {'answer_id': 70281, 'author': 'Skizz', 'author_id': 1898, 'author_profile': 'https://Stackoverflow.com/users/1898', 'pm_score': 4, 'selected': True, 'text': "<p>Rather than adding the rows of the data grid one at a time, use the <code>DataGridView.Rows.AddRange</code> method to add all the rows at once. That should only update the display once. There's also a <code>DataGridView.Columns.AddRange</code> to do the same for the columns.</p>\n"}, {'answer_id': 212197, 'author': 'Brian Hasden', 'author_id': 28926, 'author_profile': 'https://Stackoverflow.com/users/28926', 'pm_score': 2, 'selected': False, 'text': '<p>This worked for me. </p>\n\n<p><a href="http://www.syncfusion.com/faq/windowsforms/search/558.aspx" rel="nofollow noreferrer">http://www.syncfusion.com/faq/windowsforms/search/558.aspx</a></p>\n\n<p>Basically it involves deriving from the desired control and setting the following styles.</p>\n\n<pre><code>SetStyle(ControlStyles.UserPaint, true);\nSetStyle(ControlStyles.AllPaintingInWmPaint, true); \nSetStyle(ControlStyles.DoubleBuffer, true); \n</code></pre>\n'}, {'answer_id': 10887964, 'author': 'Jon', 'author_id': 1435997, 'author_profile': 'https://Stackoverflow.com/users/1435997', 'pm_score': 3, 'selected': False, 'text': "<p>People seem to forget a simple fix for this:</p>\n\n<pre><code>Object.Visible = false;\n\n//do update work\n\nObject.Visible = true;\n</code></pre>\n\n<p>I know it seems weird, but that works. When the object is not visible, it won't redraw itself. You still, however, need to do the <code>begin</code> and <code>end</code> update.</p>\n"}, {'answer_id': 45604941, 'author': 'Ramgy Borja', 'author_id': 7978302, 'author_profile': 'https://Stackoverflow.com/users/7978302', 'pm_score': 0, 'selected': False, 'text': '<p>You may also try this, its work.</p>\n\n<pre><code>public static void DoubleBuffered(Control formControl, bool setting)\n{\n    Type conType = formControl.GetType();\n    PropertyInfo pi = conType.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);\n    pi.SetValue(formControl, setting, null);\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/63974', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5363/']