Virtual Grid
OBJECTIVE GRID FOR .NET includes a
On this page:
Create a Solution for the Virtual Grid Project
1. Start Visual Studio.
2. From the File menu, select File | New | Project.
3. Select Visual C#, then choose Windows Forms Application.
- For Name, enter Virtual Grid.
- Select a Location on your hard drive.
4. Click OK to create the project. The Visual Studio project wizard generates a project template according to your settings.
Place a GridControl on Form1.cs.
1. Select the View | Solution Explorer menu option to view the Solution Explorer.
2. In Solution Explorer, double-click Form1.cs to view the form in design mode.
3. Size the form as desired.
4. Select the View | Toolbox menu option to view the Windows Forms controls toolbox.
5. Drag a GridControl from the Toolbox onto the form.
6. Size the GridControl to fill the form.
7. With the GridControl selected in the form, set the following GridControl properties.
- BorderStyle =
Fixed3D - Anchor =
Top, Left, Bottom, Right
Create a Data Structure for Storing Grid Cell Values
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.
1. Select the View | Class View menu option to view the class view for the project.
2. Right-click Virtual Grid project name, and then select Add | Class from the context menu.
3. Enter DataArray.cs for the class name, then click Add.
4. View the code for DataArray.cs. If it did not appear after you clicked Add, double click DataArray.cs in the Solution Explorer, and then select the View | Code menu option.
5. Change the DataArray class definition so that it implements ISerializable:
public class DataArray : ISerializable
{
public DataArray()
{
//
// TODO: Add constructor logic here
//
}
}
6. 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;
7. Add a private hash table data member to store the grid data:
public class DataArray : ISerializable
{
private Hashtable data = new Hashtable();
public DataArray()
...
8. 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).
9. 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));
}
10. 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 Virtual Grid Support to the Project
1. Add following using declarations:
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Stingray.Grid;
2. Add a main menu for your application:
3. View Form1.cs in design view.
4. Drag a MainMenu from the toolbox onto the form.
5. Click on the menu text “Type Here”. Add the following menu text: &File.
6. A new box appears below the new File menu. Add the following menu items to the File menu:
&Save
E&xit
7. Select the &Save menu item and change its properties as follows:
Name = FileSave
8. Select the E&xit menu item and change its properties as follows:
Name = FileExit
9. Add member variables for storing your data:
private DataArray data = new DataArray();
private Stream s = null;
10. 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);
Add Event Handlers for the Menu Commands and Virtual Grid Events
1. View Form1.cs in design view.
2. Select the GridControl in the designer by either selecting gridControl1 in the Properties list or by clicking any cell in the grid.
3. Click the lightening bolt in the grid properties window to view the OBJECTIVE GRID FOR .NET events.
4. Double-click the GetStyleRowCol event.
5. 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];
}
}
6. View Form1.cs in design view.
7. Double-click the StoreStyleRowCol event.
8. 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;
}
9. View Form1.cs in design view.
10. Click the File menu.
11. Double-click the Save menu option.
12. 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);
}
13. View Form1.cs in design view.
14. Click the File menu.
15. Double-click the Exit menu option.
16. Add the following event handling code for this menu option:
private void FileExit_Click(object sender,
System.EventArgs e)
{
this.Close();
}
17. Compile and run your virtual grid application.