Xceed Grid for WinForms v4.3 Documentation
What is a cell?
Welcome to Xceed Grid for WinForms v4.3 > Basic Concepts > Grid elements > What is a cell?

A cell is the most basic grid element and is represented by the Cell class. It is used to display text and graphical data and is contained within any row that derives from the CellRow class. 

The data type of the values displayed in cells are inherited from the corresponding cell's parent column's data type. For example, if the value of a column's DataType property is System.String, then all the cells in the same column will display string values. The cells contained in a ValueRow are the only exceptions to this case. Although their data types are, by default, the same as their parent column's, this relationship is not enforced.

Accessing cells

Each row that derives from the CellRow class has a collection of cells that can be accessed via the Cells property. Each cell can be accessed using either its index or parent column's field name. For example, to change the color of the first cell in the first data row, either line of code could be used:

C# Copy Code

grid.DataRows[ 0 ].Cells[ 0 ].BackColor = Color.LightBlue; 
grid.DataRows[ 0 ].Cells[ grid.Columns[ 0 ].FieldName ].BackColor = Color.Pink; 
grid.DataRows[ 0 ].Cells[ "ShipAddress" ].BackColor = Color.Magenta;

Keep in mind that the row whose cells you want to access must be created beforehand otherwise an ArgumentOutOfRangeException will be thrown.

Manipulating cells

Once the grid is created, each cell can be accessed via each CellRow's collection of cells. For example, to set the background color of the first cell in each DataRow to blue, the following code could be used:

C# Copy Code

foreach( DataRow row in gridControl1.DataRows )
{
  row.Cells[ 0 ].BackColor = Color.Blue;
}

Of course, if all the cells in a column are to be modified, then it would be more efficient to set the parent column's BackColor property to blue thereby setting the background color of all the cells in the column to blue (ambientness). 

Any modifications made to a cell in the DataRowTemplate before the grid is populated, will be propagated to each cell in the grid once the grid is populated. For example, if the BackColor property of the first cell in the DataRowTemplate is set to blue, then the first cell of each DataRow created by the DataRowTemplate will be blue. When using the Grid Designer, this is done transparently, however in code these manipulations, as well as the assignation of the data source, would need to be made between calls to the grid's BeginInit and EndInit methods. 

For example, to subscribe each cell in the DataRowTemplate the Click event, the following steps are required:

  1. Call the grid's BeginInit method. 

  2. Bind the grid. Binding the grid to a data source between calls to BeginInit and EndInit will read the column definitions (not their values) from the data source and thus allow you to access each cell in the DataRowTemplate. Binding the grid outside of calls to BeginInit and EndInit would have still subscribed each cell in the DataRowTemplate to the Click event however the grid would have been already populated and therefore none of the cells will receive the Click events until the grid is recreated. 

  3. Subscribe each cell in the DataRowTemplate to the Click event. 

  4. Call the grid's EndInit method.

C# Copy Code

gridControl1.BeginInit(); 

gridControl1.DataSource = dataSet11;
gridControl1.DataMember = "Orders"; 

foreach( DataCell cell in gridControl1.DataRowTemplate.Cells )
{
  cell.Click += new EventHandler( this.cell_Click );

gridControl1.EndInit(); 

private void cell_Click( object sender, EventArgs e )
{
  ( ( DataCell )sender ).BackColor = Color.Blue;
}