Using SyncFusion Grid in Virtual Mode with C# and .NET


Introduction

A few years ago a guy by the name of Stefan Hoenig came out with a virtual grid control called Objective Grid which allowed developers to greatly enhance their GUI development where a grid was needed.Later Objective Grid was sold to StingRay.  The Grid was an intricate MFC/Visual C++ library that allowed you to do everything from filling a simple cell with data, to adding controls, hiding rows, and many other excel-like features.So basically, if you bought StingRay Grid and figured out how the heck to program it, you could get grid functionality into your application.The problem with the StingRay grid is you may have found yourself weeding through hours of figuring out CellStyles, complex inheritance hierarchies, and various long method names. If it wasn't for the FAQ sheet, you were probably dead in the water.  Even tracing through the code with a debugger left you a bit stymied.

Fairly recently, Hoenig formed a new company called SyncFusion which ported the StingRay Grid into .NET.I would say that the port was a vast improvement over the previous version.I was up and running with SyncFusion in a day, and yes, the FAQ sheet is still there.(Admittedly it helps to have used the previous version, StingRay Grid in understanding SyncFusion's world of styles and events).



Figure 1 - Sync Fusion Virtual Grid

Virtual Grid Mode

The SyncFusion Grids virtual mode allows you to map a large piece of data to the Grid.  All data displayed inside the grid is drawn using the data you choose to map.  The grid allows you to scroll to any piece of your data, even if it is 1,000,000 records long.  So how does SyncFusion accomplish this mapping?  Anytime the grid needs to redraw a piece of data, it fires an event called QueryCellInfo.  This event passes in the row and column necessary to fill and the cell's style.  All the user needs to worry about is assigning the cell that is being queried with their data structure.  The grid doesn't care where the data comes from, only what the cell style should be when it queries for it.  In order to tell the grid how virtual it needs to be (anotherwords how large), the user also needs to trap the events QueryRowCount and QueryColCount (Col stands for column as opposed to say... colonel).  Despite the occasional strange method names, the grid is well designed, extremely stable, and as you can see, a snap to get running in virtual mode.

Sample Virtual Grid

In our example we created a sample virtual grid to display addresses.  Below is the UML design of our sample Form and associated set of Address objects. Each address contains the data for an address and a string array containing the address data.  The Data array is used for filling the grid and for sorting.

Figure 2 - UML Diagram-Reverse engineered using WithClass

The Event Handlers

The event handlers for our simple grid example are shown below.  These are the crux of tying your data to the virtual grid.  QueryCellInfo checks to make sure that we are not sitting in the column header or row header and then proceeds to populate the queried cell with the value from the addressData cell.  The QueryRowCount event handler sets the virtual row count of the grid to the number of elements in the address data structure.

Listing 1 - Event Handlers needed in a Sync Fusion Grid to give it Virtual Behavior

/// <summary>
///
Fill the cell with data hear upon request from the grid
/// </summary>
private void gridControl1_QueryCellInfo(object sender, Syncfusion.Windows.Forms.Grid.GridQueryCellInfoEventArgs e)
{
// Make sure the cell falls inside the grid
if (e.RowIndex > 0 && e.ColIndex > 0)
{
// Assign the address data to the cell
e.Style.CellValue = ((Address)_addressData[e.RowIndex-1]).Data[e.ColIndex-1];
// we handled it, let the grid know
e.Handled = true;
}
}
/// <summary>
///
Set the number of virtual rows based on the address data
/// </summary>
private void gridControl1_QueryRowCount(object sender, Syncfusion.Windows.Forms.Grid.GridRowColCountEventArgs e)
{
e.Count = _addressData.Count;
e.Handled =
true;
}
/// <summary>
///
Set the number columns to 7
/// </summary>
private void gridControl1_QueryColCount(object sender, Syncfusion.Windows.Forms.Grid.GridRowColCountEventArgs e)
{
e.Count = 7;
e.Handled =
true;
}

Sorting the Virtual Grid

Sorting the SyncFusion grid requires that you sort only the data since the virtual grid is a reflection of the data and doesn't actually contain the data.  In order to sort the data, we needed to make our address object contain an IComparable interface.  IComparable uses the CompareTo method to override sorting behavior.  Below is the CompareTo method for our Address object.The method uses a static field of the Address class, CompareIndex, that can be set to determine which item of the address should be sorted.The CompareTo method uses the string class's CompareTo method to compare the two strings of the current address item.

Listing 2 - CompareTo method implemented for Sorting Items in the Grid

#region IComparable Members
public int CompareTo(object obj)
{
// TODO: Add Address.CompareTo implementation
Address address1 = this;
Address address2 = (Address)obj;
return address1.Data[CompareIndex].CompareTo(
address2.Data[CompareIndex]);
}
#endregion 

Sorting of the grid can be handled in the CellClick event handler provided by SyncFusion.  You just need to call sort on the column header that is clicked by the user (which is in row 0).

Listing 3 - Handling Sorting in the CellClick Event Handler

private void gridControl1_CellClick(object sender,Syncfusion.Windows.Forms.Grid.GridCellClickEventArgs e)
{
// if clicked in header, sort
if ((e.RowIndex == 0) && (e.ColIndex > 0))
{
// first set the column you want to sort on
Address.CompareIndex = e.ColIndex - 1;
// call sort on the Address ArrayList,
// which will use CompareTo to sort
_addressData.Sort();
// force the grid to refresh after sorting
gridControl1.Refresh();
}

Conclusion

SyncFusion has a powerful set of components for allowing you to enhance your .NET experience.Among them is the SyncFusion grid which contains a myriad of functionality.You can use the SyncFusion grid component in a virtual mode to reflect your data.Virtual mode is particularly useful for tremendously large amounts of data which don't necessarily need to come from memory. Anyway, I hope this article helps you sync up your world with C# and .NET.