Need help showing a wait cursor, image and/or message while gridview data is loading and formatting

3 posts, 0 answers
  1. Patrick Baggett
    Patrick Baggett avatar
    9 posts
    Member since:
    Aug 2012

    Posted 18 Jul 2012 Link to this post

    I've tried setting the wait cursor, gridview.tableElement.text and font properties before initiating data load process and then resetting to empty\default once data is loaded.  No luck using this approach and I can't seem to find much documentation on this.  Thought of approaching this by retrieving data using a separate thread - is this even possible? 

    Any help would be greatly appreciated.

    thanks
  2. erwin
    erwin avatar
    358 posts
    Member since:
    Dec 2006

    Posted 20 Jul 2012 Link to this post

    Hi Patrick,

    I use the following code in many projects:
    #region richard IT consulting
    /*
    *  $Header: /RichardConsulting.root/RichardConsulting/RichardConsulting.Utilities/UI/WaitCursor.cs 1     20.04.11 21:31 Richard $
    *
    *  (c)2006-2010 richard IT consulting
    *          written by Erwin Richard
    *          mailto:info@richardconsulting.ch
    *
    */
    #endregion
    using System;
    using System.Windows.Forms;
    using System.Threading;
     
    namespace RichardConsulting.Utilities.UI
    {
        /// <summary>
        /// Manages a Wait Cursor
        /// </summary>
        /// <remarks>
        /// <code>
        /// using(WaitCursor wait = new WaitCursor())
        /// {
        ///     // insert lengthy operation code here
        /// }
        /// </code>
        /// or even shorter
        /// <code>
        /// using(WaitCursor.Create())
        /// {
        ///     // lengthy code
        /// }
        /// </code>
        /// </remarks>
        public class WaitCursor : IDisposable
        {
     
            private static int refCount = 0;
     
            public WaitCursor()
            {
                Interlocked.Increment(ref refCount);
                Cursor.Current = Cursors.WaitCursor;
            }
     
            /// <summary>
            /// returns an IDisposable WaitCursor instance
            /// </summary>
            /// <returns></returns>
            public static IDisposable Create()
            {
                return new WaitCursor();
            }
     
            ~WaitCursor()
            {
                this.Dispose(false);
            }
     
            protected virtual void Dispose(bool disposing)
            {
                if (Interlocked.Decrement(ref refCount) == 0)
                {
                    Cursor.Current = Cursors.Default;
                }
            }
     
            #region IDisposable Members
     
            public void Dispose()
            {
                this.Dispose(true);
                GC.SuppressFinalize(this);
            }
            #endregion
        }
    }

    An advantage is that you don't need a reference to a control or form.
    Nested lengthy passages are handled quite well too - if you use other cursors than standard and wait/hourglass, you have to extend the functionality a bit to save/restore the previous cursor.

    Regards
    Erwin
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 23 Jul 2012 Link to this post

    Hello guys,

    @Erwin - thank you for sharing your code with the community. I have updated your Telerik points for your time and effort.

    @Patrick - you can use the Erwin code for a cursor. Alternatively, here is a small sample demonstrating loading data into a DataTable in a background thread and then assigning it to RadGridView:
    public partial class Form1 : Form
      {
     
          BackgroundWorker worker;
          DataTable table;
          public Form1()
          {
              InitializeComponent();
          }
     
          private void radButton1_Click(object sender, EventArgs e)
          {
              radGridView1.TableElement.Font = new System.Drawing.Font("Arial", 20);
              radGridView1.Cursor = Cursors.WaitCursor;
              radGridView1.TableElement.Text = "Loading data";
     
              table = new DataTable();
     
              worker = new BackgroundWorker();
              worker.DoWork += new DoWorkEventHandler(worker_DoWork);
              worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
              worker.RunWorkerAsync();
          }
     
          void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
          {
              radGridView1.DataSource = table;
               
              radGridView1.TableElement.ResetValue(LightVisualElement.FontProperty, Telerik.WinControls.ValueResetFlags.Local);
              radGridView1.Cursor = Cursors.Arrow;
              radGridView1.TableElement.ResetValue(LightVisualElement.TextProperty, Telerik.WinControls.ValueResetFlags.Local);
          }
     
          void worker_DoWork(object sender, DoWorkEventArgs e)
          {
              table.Columns.Add("col");
              for (int i = 0; i < 400000; i++)
              {
                  table.Rows.Add(i);
              }
          }
      }

    One more approach to notify the user of a time taking process is RadWaitingBar. Sample is available here: http://www.telerik.com/help/winforms/track-and-status-controls-waitingbar-using-radwaitingbar-with-a-background-worker.html.

    I hope that you find this information useful.

    Regards,
    Stefan
    the Telerik team
    RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
Back to Top