Note that there are some explanatory texts on larger screens.

plurals
  1. PODataGridView selection change doesn't show until the SelectionChanged event handler has completed
    primarykey
    data
    text
    <p>I'm catching the DataGridView control's SelectionChanged event and my event handler takes about 1/2 a second to complete its task (setting the values of several controls, etc.). In UI terms, that's an eternity.</p> <p>The problem is that the DataGridView's user interface doesn't update the selection immediately. What I want is for the user to see the selection change in the DataGridView as soon as they click and <strong><em>then</em></strong> the lengthy work to be done. It will still take 1/2 a second to complete the whole task and the UI will not be responsive during that time, and that's OK -- at least the user will get immediate feedback.</p> <p>You can see this behaviour by inserting the following code into a new Form1 class:</p> <pre><code> private System.Windows.Forms.DataGridView dataGridView1; public Form1() { InitializeComponent(); dataGridView1 = new DataGridView(); dataGridView1.Dock = DockStyle.Fill; dataGridView1.Columns.Add("Column0", "Column 0"); dataGridView1.Rows.Add("Row 0"); dataGridView1.Rows.Add("Row 1"); dataGridView1.Rows.Add("Row 2"); dataGridView1.Rows.Add("Row 3"); dataGridView1.SelectionChanged += new EventHandler(dataGridView1_SelectionChanged); this.Controls.Add(dataGridView1); } private void dataGridView1_SelectionChanged(object sender, EventArgs e) { if (this.Handle != null) { this.DoSomethingForAWhile(); // Even BeginInvoke doesn't help //this.BeginInvoke((MethodInvoker)this.DoSomethingForAWhile); } } private void DoSomethingForAWhile() { // Do anything that causes a noticable delay DateTime t0 = DateTime.Now; while ((DateTime.Now - t0).TotalSeconds &lt; 2) { // Do nothing } } </code></pre> <p>I would normally use Control.BeginInvoke so that the paint messages could be processed before my 1/2 second task starts. This works with a ListView control but for some reason it doesn't work with DataGridView.</p> <p>I <em>could</em> do the processing on another thread and use Invoke to set the UI values, but that seems like a lot of complexity for something that <em>should</em> be a simple task.</p> <p>Surely there's a better way.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload