How do I update the datagrid so it will not affect focus?

How do I update the datagrid so it will not affect focus?

I am trying to make an application which will download records to a datagrid in the background, similar to the way e-mail appears in Outlook. I have this bound to a datagrid, and for argument sake, let's say I have a timer that appends a record to dataset once per second.

So far so good; it works as expected -- except the datagrid steals focus each time the timer fires. If I am on another form, or control, suddenly I am sitting on the grid!

I would like the record to appear without affecting focus. Any idea how to update the datagrid so it will not affect focus?

    Requires Free Membership to View

    When you register, you'll begin receiving targeted emails from my team of award-winning writers. Our goal is to provide a unique online resource for developers, architects and development managers tasked with building and maintaining enterprise applications using Visual Basic, C# and the Microsoft .NET platform.

    Hannah Smalltree, Editorial Director

    By submitting your registration information to SearchWinDevelopment.com you agree to receive email communications from TechTarget and TechTarget partners. We encourage you to read our Privacy Policy which contains important disclosures about how we collect and use your registration and other information. If you reside outside of the United States, by submitting this registration information you consent to having your personal data transferred to and processed in the United States. Your use of SearchWinDevelopment.com is governed by our Terms of Use. You may contact us at webmaster@TechTarget.com.

I hate thieves, especially those that steal focus! What you're going to need to do in the timer event handler is get the control that has focus on the form (System.Windows.Forms.Control.Controls[].Focused) and save it off. After you do your updating, call the Control.Focus() method to reset the control back.

Of course, the default Data Grid control is not so perfect and there's no easy way to keep it from stealing focus. If it still steals focus and refuses to update in the background, you may be better off looking at one of the third party grid controls.

This was first published in March 2004