Tags: clicks, cursor, datagridview, detail, forms, grids, hourglass, load, master, mouse, net, seconds, time, user, web

How to show wait cursor (hourglass) in DataGridView

On .Net » Web Forms

2,844 words with 4 Comments; publish: Wed, 19 Dec 2007 10:00:00 GMT; (10046.88, « »)

My master/detail grids take a long time to load (several seconds). I want the mouse cursor to change to an hourglass after the user clicks on the master grid (when detail grid's data is loading). How to do this?

Thanks

All Comments

Leave a comment...

  • 4 Comments
    • This is a great question. Basically you will want to use the RowLeave event and set the cursor to the WaitCursor. Then all you need to do is figure out when to set the cursor back to the default. The easiest way to do this is to use BeginInvoke since BeginInvoke will run the code at the next message pump. Here is what my first crack at this looked like:

      private void ordersDataGridView_RowLeave(...)

      {

      this.Cursor = Cursors.WaitCursor;

      ordersDataGridView.BeginInvoke((MethodInvoker)delegate()

      {

      this.Cursor = Cursors.Default;

      });

      }

      So the above code will show the wait cursor (hourglass) for the duration that it takes for your datasource to load. Now, if you also want to wait until the DataGridView has painted itself (this is separate from the datasource load) you can add one additional line of Application.DoEvents() before you set the cursor back to default. Here is the complete listing:

      private void ordersDataGridView_RowLeave(...)

      {

      this.Cursor = Cursors.WaitCursor;

      ordersDataGridView.BeginInvoke((MethodInvoker)delegate()

      {

      Application.DoEvents();

      this.Cursor = Cursors.Default;

      });

      }

      -mark

      DataGridView Program Manager

      Microsoft

      This post is provided "as-is"

      #1; Sat, 08 Sep 2007 19:16:00 GMT
    • Works great. Thank you.

      Not sure of the difference but to be a little more specific I set the grid's cursor instead of the form's:

      this.MyDataGridView.Cursor = Cursors.WaitCursor;

      #2; Sat, 08 Sep 2007 19:17:00 GMT
    • Do you really have to specify that every time you want to show the wait cursor, or is there a generic "If the computer is thinking, show this." thing?

      Sorry... I'm VERY new to this!

      #3; Sat, 08 Sep 2007 19:18:00 GMT
    • Yeah. You have to do it everytime. In every button click I always set the cursor to waitcursor if something long is about to (or could) happen - such as queryring a database. Then at the end set it back to default. It's tedious but you have lots of control.
      #4; Sat, 08 Sep 2007 19:19:00 GMT