Cell

Cell is the smallest unit is managed in the grid.
1.png

ReoGridPos

ReoGridPos is a struct used to define a position in a spreadsheet:
var pos = new ReoGridPos(2, 1);   // row: 2, col: 1
var pos = new ReoGridPos("D5");   // row: 5, col: 4

ReoGridPos can also created from an address or convert into an address as string:
ReoGridPos pos = new ReoGridPos("B2");
string address = pos.ToStringAdderss();  // D5

The following static methods are used to check whether an address is valid:
ReoGridPos.IsValidAddress("D5");       // true
ReoGridPos.IsValidAddress("A1:D5");    // false
ReoGridPos.IsValidAddress("myrange");  // false: but available to grid["myrange"] 

Set cell data

Cell data is an input value that will be used to display the text, and calculate in formula. Data can be any types. If the data as null is specified, nothing will be displayed; if the data is an object of a custom type, the method ToString of the object will be invoked.

Method

Set data into cell:
grid[0, 0] = 10;                 // number
grid[0, 1] = "string"            // string
grid[0, 2] = DateTime.Now;       // datetime
Set by a cell address also is supported:
grid["C3"] = "=A3+B3";
or set by named range: (for more about named range, see Range)
grid.DefineNamedRange("mycell", new ReoGridRange("A1"));
grid["mycell"] = 10.12345;
or set custom data type:
public class MyData { 
  public override string ToString() { return "mydata"; }
}
grid[0, 3] = new MyData();

Some types as follows, will be specially processed:
object[,]        // array of object will be copied to a range
PartialGrid      // partial grid will be copied to a range
CellBody         // instance of CellBody will be set into a cell
or use the following methods:
grid.SetCellData(5, 2, "hello world");
grid.setCellData(new ReoGridPos(5, 2), "hello world");

Set data of array to range:
grid.SetRangeData(new ReoGridRange(1, 1, 3, 3), new object[ , ] {
    { 'a', 'b', 'c' },
    { 1, 2, 3 },
    { 4, 5, 6 }
  });
or:
grid[1,1] = new object[ , ] {
    { 'a', 'b', 'c' },
    { 1, 2, 3 },
    { 4, 5, 6 }
  });
17_2.png

Action

// set data to cell at position 5,2 (row: 5, col: 2)
grid.DoAction(new RGSetCellDataAction(5, 2, "hello world"));

Script

Set cell data by script:
grid.getCell(5,2).data = 'hello world';

Cell Instance

At the beginning, when the control is created and initialized, ReoGrid does not create any instances of cell in memory. Cell instance first to be created only when the data or styles of the cell is set.

As below, the cell A1 has the instance but B1 hasn't.
2.png

Get cell instance

Get cell instance at position 2,3:
var cell = grid.GetCell(2,3);
Cell can be null if there is no data or styles attached, it is recommended to do a check before using an instance:
if (cell != null) {
    ...
}

It is possible to create a cell instance without any data or styles setting, the method CreateAndGetCell will create an empty cell instance before returning it. Since many instances exist and managed in spreadsheet will take many of memory, ReoGrid does not recommend user-code to use this method except for some specific reason.
var cell = CreateAndGetCell(2, 3);

From v0.8.6, the collection of cell instances is available.
var cell = grid.Cells[1, 2];        // cell in 1,2
var cell = grid.Cells["C2"];        // cell in 1,2
var cell = grid.Cells["myname"];    // cell from named range

Cells collection always return an existing instance of cell, if an instance does not exist, it will be created intermediately, it is totally same as CreateAndGetCell. This routine is easy to use but be careful it has also the memory usage and performance issue, for example:
if(grid.Cells[1, 2].Data == null) { ... }   // this will cause an instance of cell be created
if(grid.GetCellData(1, 2) == null) { ... }  // nothing will be created, just read data from a cell

Auto Data Format

When data to be set into a cell at the first time, ReoGrid try to test the data and find a best type to format the data. If the data be detected as number or percent, the text horizontal alignment of the cell will be set to right automatically.

To disable this behavior, set Edit_AutoFormatCell of the control setting to false.
grid.SetSettings(ReoGridSettings.Edit_AutoFormatCell, false);
Learn more about Data Format and Settings.

Auto data type conversion

ReoGrid will convert a data into others data type in order to use the data in formula calculation quickly. If string "10" to be set into a cell which has a 'Number' format specifying, the data of cell will be converted into double instead of specified data automatically. To avoid this conversion and save "10" as string in memory, change the data-format of the cell to 'Text'.

// generic-cell-data-format will convert the value into number automatically
grid[3, 1] = "10"; // "10" -> 10 auto convert
AssertEquals(grid.GetCellData<int>(3, 1), 10);

// set 3,2 to text
grid.SetRangeDataFormat(3, 2, 1, 1, DataFormat.CellDataFormatFlag.Text, null);
grid[3, 2] = "10"; // "10" without auto convert
AssertEquals(grid.GetCellData<int>(3, 2), 10);
AssertEquals(grid.GetCellData<double>(3, 2), 10d);

Text overflow (Cells Clipping)

Like Excel, ReoGrid will display a cells text overflowing its cell.

Next: Rows & Columns

Last edited Apr 21, 2014 at 7:59 AM by unvell, version 22