Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7990121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:54:58+00:00 2026-06-04T12:54:58+00:00

I’m catching the DataGridView control’s SelectionChanged event and my event handler takes about 1/2

  • 0

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.

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 then 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.

You can see this behaviour by inserting the following code into a new Form1 class:

    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 < 2)
        {
            // Do nothing
        }
    }

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.

I could 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 should be a simple task.

Surely there’s a better way.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T12:54:59+00:00Added an answer on June 4, 2026 at 12:54 pm

    Whilst I think it’s appropriate to stick your long running task into a BackgroundWorker and callback to the UI to update it, you can get the behaviour you want with a call to DataGridView.Refresh(), and use of BeginInvoke;

    private void dataGridView1_SelectionChanged(object sender, EventArgs e)
    {
        dataGridView1.Refresh();
        if (this.Handle != null)
        {
            this.BeginInvoke((MethodInvoker)this.DoSomethingForAWhile);
        }
    }
    

    This approach sticks a Paint message on the UI thread before your long running task, which is blocking the UI thread. At that point, I don’t believe the DataGridView has yet been instructed to Paint itself, hence why you weren’t seeing it update.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.