Skip to main content Skip to footer

Different Scrolling Implementations in C1TrueDbGrid

It is often required in the application/control to detect Start and End of Scroll to get a certain behaviour with respect to scrolling. This blog explains how to implement the following in C1TrueDbGrid:

  1. How to detect EndScroll in a C1TrueDbgrid
  2. Synchronize two C1TrueDbGrids' Scroll

Detect EndScroll in a C1TrueDbgrid

C1TrueDBGrid does not have a direct way of detecting EndScroll/AfterScroll. There is one Scroll event that actually occurs before C1TrueDBgrid gets a new Scroll value, so how could one go about detecting EndScroll/AfterScroll? Here is the way:

  1. Create a class inherited from C1TrueDbGrid.
  2. Override WndProc comm channel.
  3. See if Scroll has ended; if so, pass an empty value to OnScroll delegate.
  4. Within an If-contruct, or while using any other verification method, lookout for empty EventArgs being passed in the method. If so, write the code here that you wish to be implemented when Scroll has ended.

The custom class now looks like this:

class MyTDBG : C1.Win .C1TrueDBGrid .C1TrueDBGrid  
{  
   protected void OnScroll(EventArgs e)  
   {  
      if (e.Equals (EventArgs.Empty ))  
         //Place your code here.  
         Console.WriteLine("End Scroll");  
   }  

   //Verify if Scroll has ended.  
   protected override void WndProc(ref Message m)  
   {  
       base.WndProc(ref m);  
       if (m.Msg == 0x115)  
       {  
          if ((ScrollEventType)(m.WParam.ToInt32() & 0xffff) == ScrollEventType.EndScroll )  
          {  
             OnScroll(EventArgs.Empty);  
          }  
       }  
    }  
 }

To use this custom component and considering you dragged and dropped C1TrueDbGrid from the VS Toolbox, change these lines within the form’s designer:

this.c1TrueDBGrid1 = new C1.Win.C1TrueDBGrid.C1TrueDBGrid();  
private C1.Win.C1TrueDBGrid.C1TrueDBGrid c1TrueDBGrid1; 

to

this.c1TrueDBGrid1 = new MyTDBG();  
private MyTDBG c1TrueDBGrid1;

Save and close the window and rebuild the project. Should you need to verify BeginScroll, the actual C1TrueDbgrid_Scroll event is always there.

private void c1TrueDBGrid1_Scroll(object sender, C1.Win.C1TrueDBGrid.CancelEventArgs e)  
 {  
 //Your code needs to be implemented before Scroll begins.  
 }

Synchronize two C1TrueDbGrids' Scroll

Like I said earlier, Scroll event always returns values prior to the scroll. If I don't have a way to fetch final values, I simply can't synchronize another grid's scrollbar with the main C1TrueDbGrid. However, I can still outsource the job to a delegate I invoke from within the C1TrueDbGrid's Scroll event.

public delegate void InvokeDelegate();  
object ActiveGrid;  

private void c1TrueDBGrid1_Scroll(object sender, C1.Win.C1TrueDBGrid.CancelEventArgs e)  
{  
   ActiveGrid = sender;  
   //Invoke delegate to create sync between grids.  
   this.BeginInvoke(new InvokeDelegate(SyncGrids));  
}  

private void SyncGrids()  
{  
   foreach (Control ctrl in this.Controls)  
   {  
      //Set Horizontal and Vertical offsets of the second grid equal to the main grid.  
      if ((ctrl) is C1.Win.C1TrueDBGrid.C1TrueDBGrid)  
      {  
         (ctrl as C1.Win.C1TrueDBGrid.C1TrueDBGrid).Splits[0].HorizontalOffset = (ActiveGrid as C1.Win.C1TrueDBGrid.C1TrueDBGrid).Splits[0].HorizontalOffset;  
(ctrl as C1.Win.C1TrueDBGrid.C1TrueDBGrid).Splits[0].VerticalOffset = (ActiveGrid as C1.Win.C1TrueDBGrid.C1TrueDBGrid).Splits[0].VerticalOffset;  
      }  
   }  
}

We're good to scroll now:) A PFA sample that caters to both requirements is mentioned above. Download Sample

MESCIUS inc.

comments powered by Disqus