Code Samples for 10Tec .NET DataGrid

(iGrid.NET Control Tour, Step 5 of 6)

Populating the grid

10Tec WinForms grid is a cell matrix. To get it working, just create the required number of columns and rows and then assign values to the 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:

Access grid cells in cell collection by row and column indices

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 items of our grid for WinForms are indexed from zero.

Now let's populate our 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;
Fast access to rows and column by string keys

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, 10Tec WinForms grid control is 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 cells in the first column with a blue color:

Column cell style

In iGird.NET this can be done with the following statement:

iGrid1.Cols[0].CellStyle.BackColor = Color.CornflowerBlue;

We can also create the 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 the thousand separator and align them to the right:

Format grid column cells using format strings

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);

We access the cell's properties directly to get the desired view:

Format individual grid cells

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:

Cell properties inheritance

The cell inherits this setting as we have not redefined alignment in it. The color and font formatting we have made is also in effect!

This page lacks only the code snippet to implement this task:

iGrid1.Cols[2].CellStyle.TextAlign = iGContentAlignment.TopRight;

Events related to editing

iGrid.NET has its own set of events that allows you to control data entered by the user. Among them are RequestEdit, BeforeCommitEdit, AfterCommitEdit and CancelEdit. These methods work for all built-in in-place editors (textbox, combobox and checkbox). This set of events allows fine tuning of the editing process.

You can also use specific events for the text box editing feature - TextBoxKeyDown, TextBoxKeyPress, TextBoxKeyUp and TextBoxTextChanged. These events work like the standard events for the TextBox control and allow sophisticated tuning of text box editing.

Yet another special event for textbox editing, TextBoxFilterChar, can be used to simplify your work when you need to accept/reject characters entered by the user in a text box cell.

Additional screenshots, or a better DataGridView control »