This is a migrated thread and some comments may be shown as answers.

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

2 Answers 585 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Patrick Baggett
Top achievements
Rank 2
Patrick Baggett asked on 18 Jul 2012, 06:32 PM
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 Answers, 1 is accepted

Sort by
0
erwin
Top achievements
Rank 1
Veteran
Iron
answered on 20 Jul 2012, 11:58 AM
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
0
Stefan
Telerik team
answered on 23 Jul 2012, 08:40 AM
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 >>
Tags
GridView
Asked by
Patrick Baggett
Top achievements
Rank 2
Answers by
erwin
Top achievements
Rank 1
Veteran
Iron
Stefan
Telerik team
Share this question
or