iGrid.NET Control Tour - Code Samples
Populating the grid
iGrid.NET is a cell matrix, and to get it working, simply create the required number of columns and rows and then assign values to iGrid.NET's cells:
iGrid1.Cols.Count = 3;
iGrid1.Rows.Count = 5;
iGrid1.Cells[2, 0].Value = "Test value";
iGrid1.Cells[3, 0].Value = 12345;
| The code above creates a grid with 3 columns and 5 rows and changes the values in its two cells. Notice how easily you can access each cell through the Cells collection. The Cols and Rows collections are used the same way to access iGrid's columns and rows. Also notice that we store values of different data types in cells of the same column without any additional work. |
|
 |
Like in the .NET Framework, all iGrid.NET's items are indexed from zero.
Now let's create a more complex grid:
for(int i=0; i<=2; i++)
iGrid1.Cols.Add("col" + i.ToString(), "Column " + i.ToString());
iGrid1.Rows.Count = 5;
for(int i=0; i<iGrid1.Rows.Count; i++)
for(int j=0; j<iGrid1.Cols.Count; j++)
iGrid1.Cells[i, j].Value = (i+1)*10000 + j;
 |
|
This code creates the grid shown at left. Each column and row in iGrid can have its own string key, and we used it when we created our columns with the Add method of the Cols collection. That overloaded version of the method accepts a column's string key as its first parameter and the column's caption as the second one. |
After that we can access each cell not only by its numeric index but by its column's string keys too. For instance, the following statement displays "40002" (column and row keys are case-insensitive):
MessageBox.Show(iGrid1.Cells[3, "COL2"].Value.ToString());
Working with cell styles
| As we said earlier, iGrid.NET was built on cell styles. By default, each cell does not have its own cell style object and inherits the properties of the cell style object of the column it belongs to. You can use this fact to reformat the entire column in one statement. Let's say we need to highlight the first column with a blue color like on the screenshot. In iGird.NET this can be done with the following statement: |
|
 |
iGrid1.Cols[0].CellStyle.BackColor = Color.CornflowerBlue;
 |
|
We can also create a required style as a standalone object on the form's level and apply it to several columns to get the same formatting in those columns. As an example, let's format the values in the second and third columns as currency values with thousand separator and align them to right. |
First, we define the following cell style object in our form:
private iGCellStyle CurrencyCellStyle = new iGCellStyle();
Then we set the required properties in it and simply apply this style to the 2nd and 3rd columns:
CurrencyCellStyle.TextAlign = iGContentAlignment.TopRight;
CurrencyCellStyle.FormatString = "{0:$#,##}";
iGrid1.Cols[1].CellStyle = CurrencyCellStyle;
iGrid1.Cols[2].CellStyle = CurrencyCellStyle;
That's all! Custom formats in iGrid.NET are specified as if you used the String.Format method from the .NET Framework.
Formatting individual cells
If you need to format an individual cell, you can define a cell style object with required formatting and apply it to this cell. But there is a simpler way when you format a cell through its properties. Look at this code:
iGrid1.Cells[1, 2].BackColor = Color.Magenta;
iGrid1.Cells[1, 2].ForeColor = Color.White;
iGrid1.Cells[1, 2].Font = new Font("Tahoma", 8, FontStyle.Bold);
| In this code snippet we access the cell's properties directly to get the desired view (see the screenshot at right). However, if you need to format several cells using the same formatting, we recommend that you use a cell style object for this purpose as it will simplify your code and its performance. |
|
 |
Inheritance in cell properties
By default, all formatting properties of a cell are set to a special 'NotSet' value. It means that the value of the corresponding property is inherited from the cell style object of the column that the cell belongs to.
 |
|
In the previous sample we could right-align the values in the last column using the column's cell style object, and the formatted cell in the 2nd row would accept this formatting option. The cell inherits this setting as we have not redefined alignment in it. It also saves the formatting we've done in it! |
This page lacks only the code snippet to implement this task:
iGrid1.Cols[2].CellStyle.TextAlign = iGContentAlignment.TopRight;
|