Services designed to fit your needs, mitigate your risk and deliver results.

Developer Exchange Blog

  • Home
    Home This is where you can find all the blog posts throughout the site.
  • Articles
    Articles Displays all articles posted on this site.
  • Bloggers
    Bloggers Search for your favorite blogger from this site.

Janus GridEX

by

This example will show how to create columns containing checkboxes within a Janus GridEX, and more importantly, how to display the checkbox only when desired. For our example, two columns of Booleans are created.

The following test data is used:

public class TestDataSource
{
private bool _purchase;
private bool _include;
public TestDataSource(bool IncludeFlag, bool PurchaseFlag)
{
_include = IncludeFlag;
_purchase = PurchaseFlag;
}
#region properties
public bool bIncludeFlag
{
get { return _include; }
set { _include = value; }
}
public bool bPurchasedFlag
{
get { return _purchase; }
set { _purchase = value; }
}
#endregion
}

To support a Janus GridEX, we use a singleton that provides a property that returns a binding source ‘bsTestDataSource “. This binding source is provided to the grid as the ‘datasource”.

public class TestDataSourceTool
{
private List _lTestDataSource = null;
private BindingSource _bindingsource;
#region singleton functionality
private static readonly TestDataSourceTool _instance = null;

static TestDataSourceTool()
{
_instance = new TestDataSourceTool();
}
#endregion

public TestDataSourceTool()
{
_lTestDataSource = new List();

_bindingsource = new BindingSource();
_bindingsource.DataSource = _lTestDataSource;

for (int i = 0; i < 5; i++)
{
bool odd = (i% 2 == 0) ? true : false;
_lTestDataSource.Add(new TestDataSource(odd,!odd));
}
}
#region properties

public static TestDataSourceTool Instance
{
get { return _instance; }
}
public BindingSource bsTestDataSource
{
get { return _bindingsource; }
}
#endregion
}

Data setup is the first step in creating the grid. The following code snippet defines the display position within the grid, the property name used for binding, and the text to display as the caption. Note: Strings that represent data property names must match exactly!

private enum GridColumnPosition
{
INCLUDE_FLAG = 0,
PURCHASED_FLAG = 1,
}

public static readonly String INCLUDE_FLAG = "bIncludeFlag";
public static readonly String PURCHASED_FLAG = "bPurchasedFlag";

public static readonly String INCLUDE_FLAG_CAPTION = "Enabled";
public static readonly String PURCHASED_FLAG_CAPTION ="Purchased";

Next the grid is defined. We use the grid “datasource” binding in our example. The RetrieveStructure(true) will examine the data source for all properties and automatically create the two columns specified by the “public bool bIncludeFlag” and “public bool bPurchasedFlag“.

gridEX1.Hierarchical = true;
gridEX1.DataSource = TestDataSourceTool.Instance.bsTestDataSource;
gridEX1.RetrieveStructure(true);

Next is the definition of the columns within the grid. The definition will use the previously defined position, names and caption for each column. Note: Each RootTable.Columns is referenced by name and must match the property names from the datasource exactly!

GridEXColumn column:
column = gridEX1.RootTable.Columns[INCLUDE_FLAG];
column.OwnerDrawnMode = ColumnOwnerDrawnMode.Cells;
column.EditType = EditType.CheckBox;
column.Tag = GridColumnPosition.INCLUDE_FLAG;
column.Caption = INCLUDE_FLAG_CAPTION;
column.Width = 70;

column = gridEX1.RootTable.Columns[PURCHASED_FLAG];
column.OwnerDrawnMode = ColumnOwnerDrawnMode.Cells;
column.EditType = EditType.CheckBox;
column.Tag = GridColumnPosition.PURCHASED_FLAG;
column.Caption = PURCHASED_FLAG_CAPTION;
column.Width = 70;

At this point, the grid is testable. The two columns should be displayed with checkboxes in each cell.

janus_gridex_form1

Remember the example is to display only the odd rows of the “Enabled” column, and the even columns of the “Purchase” column. To control the display of each column/row cell we need to add two events to the grid definition, DrawGridArea and EditingCell.

gridEX1.DrawGridArea += new DrawGridAreaEventHandler(gridEX1_DrawGridArea);
gridEX1.EditingCell += new EditingCellEventHandler(gridEX1_EditingCell);

The DrawGridArea event will determine which column and row is drawn and handle the rendering accordingly. If it is a cell of interest, draw only the background and not the foreground, thus hiding the checkbox.

void gridEX1_DrawGridArea(object sender, DrawGridAreaEventArgs e)
{
GridEXRow row = (GridEXRow)e.Row;
GridColumnPosition col = (GridColumnPosition)e.Column.Tag;
bool odd_even = (row.AbsolutePosition % 2 == 0) ? true : false;
if (col == GridColumnPosition.INCLUDE_FLAG && odd_even)
{
e.PaintBackground();
e.Handled = true;
}
if (col == GridColumnPosition.PURCHASED_FLAG && !odd_even)
{
e.PaintBackground();
e.Handled = true;
}
}

The DrawGridArea event will “hide’ the check box in the appropriate column/row, but the checkbox is still there. If the user were to select that column/row position, the checkbox would still allow selection. To control this action, the EditingCell event is used. The EditingCell event will determine the column/row cell and use the same criteria as the DrawGridArea to determine editing. If it meets the criteria, the edit event is cancelled, allowing no changes to occur.

void gridEX1_EditingCell(object sender, EditingCellEventArgs e)
{
GridEXRow row = gridEX1.CurrentRow;
GridColumnPosition col = (GridColumnPosition)e.Column.Tag;
bool odd_even = (row.AbsolutePosition % 2 == 0) ? true : false;

if (col == GridColumnPosition.INCLUDE_FLAG && odd_even)
e.Cancel = true;

if (col== GridColumnPosition.PURCHASED_FLAG && !odd_even)
e.Cancel = true;

Final result: Enabled checkboxes are only displayed in rows 1, 3 and 5 and purchased checkboxes in row 2 and 4. Cells between the checkbox are selectable, but no other action is allowed.

janus_gridex_form2

Barry Lenox has not set their biography yet
Author's recent posts