Objective Grid for Microsoft .NET includes a sample virtual grid application. It is in the Samples subdirectory and is named Virtual Grid. The following steps explain how to create this project.
Start Visual Studio .NET.
From the File menu, select File | New | Project.
In the Project Types dialog, select Visual C# Projects.
For Templates, choose Windows Application.
For Name, enter Virtual Grid.
Select a Location on your hard drive.
Click OK to create the project. The Visual Studio .NET project wizard generates a project template according to your settings.
Select the View | Solution Explorer menu option to view the Solution Explorer.
In Solution Explorer, double-click Form1.cs to view the form in design mode.
Size the form as desired.
Select the View | Toolbox menu option to view the Windows Forms controls toolbox.
Drag a GridControl from the Toolbox onto the form.
Size the GridControl to fill the form.
With the GridControl selected in the form, set the following GridControl properties.
BorderStyle = Fixed3D
Anchor = Top, Left, Bottom, Right
The class is implemented using a hash table, which hashes the cell values based on the row and column coordinates for the cell. The class implements ISerializable, so that it can be persisted. An indexer makes access to the grid data from our grid application more convenient.
Select the View | Class View menu option to view the class view for the project.
Right-click Virtual Grid project name, and then select Add | Add Class from the context menu.
In the C# Class Wizard:
For Class Name, enter DataArray.
Click Finish.
View the code for DataArray.cs. If it did not appear after you clicked Finish, double click DataArray.cs in the Solution Explorer, and then select the View | Code menu option.
Change the DataArray class definition so that it implements ISerializable:
public class DataArray : ISerializable { public DataArray() { // // TODO: Add constructor logic here // } } |
At the top of the source file, add using declarations for System.Collections and System.Runtime.Serialization:
using System; using System.Collections; using System.Runtime.Serialization; |
Add a private hash table data member to store the grid data:
public class DataArray : ISerializable { private Hashtable data = new Hashtable(); public DataArray() ... |
Add code for an indexer to access the cell data by row and column indexes:
/// <summary> /// This indexer allows a cell value to be hashed based on /// the row and column indexes for the grid cell. A string /// key is formed using the row and column indexes. The value /// is then inserted into or obtained from the hash table /// using this key. /// The key is formed as, Row,Col. For example, cell 1,1 /// is hashed with the key, "1,1". Cell 10,5 is hashed with /// the key, "10,5". /// </summary> public String this[int x, int y] { get { // Form the hash key and an initially empty return // value string. String rc = ""; String key = String.Format("{0},{1}", x, y); // If the data for the cell is present, then get it // from the table. if (data.Contains(key)) rc = (String) data[key]; return rc; } set { // Form the key String key = String.Format("{0},{1}", x, y); // If the table already contains the key... if (data.Contains(key)) { // If the cell data is non-empty, store it if (value.Length > 0) data[key] = value; // Else if the new value is empty, remove // the key and value from the table else data.Remove(key); } // Else the table does not contain the key else { // Store the value only if it is non-empty if (value.Length > 0) data.Add(key, value); } } } |
The hash table key and value types are both .NET String instances. Only the cell values are hashed. The key is formatted using the row and column offsets. For example, the key for cell 1, 1 is String(1,1).
The following two methods implement serialization for the grid cell data:
public virtual void GetObjectData(SerializationInfo s, StreamingContext c) { s.AddValue("Data", data); } private DataArray(SerializationInfo s, StreamingContext c) { data = (Hashtable) s.GetValue("Data", typeof(Hashtable)); } |
The completed DataArray class should look something like the following:
using System; using System.Collections; using System.Runtime.Serialization; namespace Virtual_Grid { /// <summary> /// Summary description for DataArray. /// </summary> [ Serializable ] public class DataArray : ISerializable { private Hashtable data = new Hashtable(); public DataArray() { // // TODO: Add constructor logic here // } /// <summary> /// This indexer allows a cell value to be hashed based on /// the row and column indexes for the grid cell. A string /// key is formed using the row and column indexes. The value /// is then inserted into or obtained from the hash table /// using this key. /// The key is formed as, Row,Col. For example, cell 1,1 /// is hashed with the key, "1,1". Cell 10,5 is hashed with /// the key, "10,5". /// </summary> public String this[int x, int y] { get { // Form the hash key and an initially empty return // value string. String rc = ""; String key = String.Format("{0},{1}", x, y); // If the data for the cell is present, then get it // from the table. if (data.Contains(key)) rc = (String) data[key]; return rc; } set { // Form the key String key = String.Format("{0},{1}", x, y); // If the table already contains the key... if (data.Contains(key)) { // If the cell data is non-empty, store it if (value.Length > 0) data[key] = value; // Else if the new value is empty, remove // the key and value from the table else data.Remove(key); } // Else the table does not contain the key else { // Store the value only if it is non-empty if (value.Length > 0) data.Add(key, value); } } } public virtual void GetObjectData(SerializationInfo s, StreamingContext c) { s.AddValue("Data", data); } private DataArray(SerializationInfo s, StreamingContext c) { data = (Hashtable) s.GetValue("Data", typeof(Hashtable)); } } } |
Add following using declarations:
using System.IO; using System.Runtime.Serialization.Formatters.Binary; using Stingray.Grid; |
Add a main menu for your application:
View Form1.cs in design view.
Drag a MainMenu from the toolbox onto the form.
Click on the menu text "Type Here". Add the following menu text: &File.
A new box appears below the new File menu. Add the following menu items to the File menu:
&Save
E&xit
Select the &Save menu item and change its properties as follows:
Name = FileSave
Select the E&xit menu item and change its properties as follows:
Name = FileExit
Add member variables for storing your data:
Select the View | Code menu option for Form1.
Create a new private DataArray attribute and a new Stream attribute.
private DataArray data = new DataArray(); private Stream s = null; |
Add constructor code to load open, and load the DataArray containing the grid data:
// // TODO: Add any constructor code after InitializeComponent call // s = File.Open("C:/temp/VirtualGrid.dat", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite); BinaryFormatter bf = new BinaryFormatter(); if (s.Length > 0) data = (DataArray) bf.Deserialize(s); |
View Form1.cs in design view.
Select the GridControl in the designer by either selecting gridControl1 in the Properties list or by clicking any cell in the grid.
Click the lightening bolt in the grid properties window to view the Objective Grid for Microsoft .NET events.
Double-click the GetStyleRowCol event.
Add the following event handling code for the event:
private void gridControl1_GetStyleRowCol(object sender, Stingray.Grid.GetStyleRowColEventArgs e) { if (e.Row > 0 && e.Col > 0) { e.Style.Value = data[e.Row, e.Col]; } } |
View Form1.cs in design view.
Double-click the StoreStyleRowCol event.
Add the following event handling code for the event:
private void gridControl1_StoreStyleRowCol(object sender, Stingray.Grid.StoreStyleRowColEventArgs e) { data[e.Row, e.Col] = e.Style.Value; e.Stored = true; } |
View Form1.cs in design view.
Click the File menu.
Double-click the Save menu option.
Add the following event handling code for this menu option:
private void FileSave_Click(object sender, System.EventArgs e) { Cell cur = this.gridControl1.CurrentCell; data[cur.Row, cur.Col] = cur.ControlText; BinaryFormatter bf = new BinaryFormatter(); s.SetLength(0); bf.Serialize(s, data); } |
View Form1.cs in design view.
Click the File menu.
Double-click the Exit menu option.
Add the following event handling code for this menu option:
private void FileExit_Click(object sender, System.EventArgs e) { this.Close(); } |
Compile and run your virtual grid application.
Copyright © Rogue Wave Software, Inc. All Rights Reserved.
The Rogue Wave name and logo, and Stingray, are registered trademarks of Rogue Wave Software. All other trademarks are the property of their respective owners.
Provide feedback to Rogue Wave about its documentation.