Documents

NextGrid & NextDBGrid

NextGrid

NextDBGrid

NextInspector

NextSheet

NextCollection

Misc

Labs

NextGrid .NET

NextGrid dot NET How To

Related Articles:

Info
  • Skill:
  • Version: 1.0.0
  • Author: Bojan Nikolic
  • Created: 2010-09-15
  • Updated: 2011-02-06
This article will learn you how to use NextGrid.NET more efficiently.

Content:


1. Add column in run-time
2. Paint even and odd rows in different colors
3. Set grid lines color dynamically
4. Access to the newly added row
5. Enable search-as-you-type for column
6. How to access to a cell



1. Add column in run-time



Best practice for adding new column into grid is to create column object, set column properties and then add it into grid.

using NextSuite;
...
TextColumn Column;

Column = new TextColumn(); // Create column object

Column.Header.Alignment = HorizontalAlignment.Center;
Column.Header.Text = "Sample Header";

nextGrid1.Columns.Add(Column); // Add column into grid


2. Paint even and odd rows in different colors



In NextGrid.NET each cell may have own background color. Color may be set by using BackColor property of Cell.

nextGrid1[2, 3].BackColor = Color.Red;


Cell's color may be set dynamically by using CellColoring event.

private void nextGrid1_CellColoring(object sender, CellEventArgs e,
    bool selected, ref Color cellColor,  ref Color bottomGridColor, ref Color rightGridColor)
{
    if (!selected)
    {
        if (e.Y % 2 == 0)
        {
            cellColor = Color.Cornsilk; // colorize every second row
        }
    }
}


Please note that CellColoring event occur for inactive cells too. To limit coloring for only active cells write:

if (y < nextGrid1.RowCount) {...


3. Set grid lines color dynamically



Painting horizontal or vertical grid lines may be useful sometime. For example for framing cells, dividing columns and similar. Example:

private void nextGrid1_CellColoring(object sender, CellEventArgs e,
    bool selected, ref Color cellColor,  ref Color bottomGridColor, ref Color rightGridColor)
{
    if (y == nextGrid1.RowCount - 1)
    {
       bottomGridColor = Color.Brown;
    }
}


4. Access to the newly added row



It is handy to have access to the last added row.

There are two solutions for that.

4.1. LastAddedRow propertyu



LastAddedRow property is update after methods such as AddRow or InsertRow are called.

nextGrid1.AddRow();
nextGrid1[0, nextGrid1.LastAddedRow].Value = 5;
nextGrid1[nextGrid1.LastAddedRow].Height = 32;


4.2. Return value of methods



Methods like LastAddedRows, InsertRow are returning a newly created row object (GridRow) so can be used for immediate access. Example:

nextGrid1.AddRow().Cells[2].Value = "test";
nextGrid1.AddRow().Visible = false;


5. Enable search-as-you-type for column



To enable incremental search-as-you-type feature for grid set Search property of Column to true. Only one column may be "search' column at time.

Searching will be performed at any non-editable column (Editing = false).

6. How to access to a cell



Beside standard this indexer for accessing cells, there is overloaded indexer too.

This indexer accepts 2 Object parameters. Both parameters may be int as in standard this indexer, but they may be CellColumnLocation and CellRowLocation too.

Table 1. CellColumnLocation
Name Meaning
First First column in Columns array.
Last Last column in Columns array.
Selected Equal to SelectedColumn property.


Example:

nextGrid1[CellColumnLocation.Selected, 1].Value = "test";


Table 2. CellRowLocation
Name Meaning
First First row in grid.
FirstVisible First visible row in grid. It may differ than First!
Last Last row in grid.
LastVisible Last visible row in grid. It may differ than Last!
Selected Equal to SelectedRow property.


Example:

nextGrid1[CellColumnLocation.First, CellRowLocation.LastVisible].Value = "test";

Was This Article Useful?

Only constructive comments, code contributions... will be publishes. Questions, non-official discussion will not be published.