This sample demonstrates how you can replace any cell used in the Grid
Grouping control by registering a different cell type. It shows a custom-header
cell type that has an extra button inside. This sample also shows the code for
handling the pointer when hovering over a cell, expanding records without moving
the current one, and much more.
This is how the sample looks.
In this sample, the My Header cell is customized in the HeaderCell class.
It contains the following three classes:
MyHeaderCellModel is inherited from GridTableColumnHeaderCellModel. It fixes the size of the button in the constructor using the following code:
ButtonBarSize = new Size(10, 10);
MyHeaderCellRenderer is inherited from GridTableColumnHeaderCellRenderer. It adds the customized cell button in its constructor using the following code:
this.AddButton(new MyCellButton(this));
MyCellButton is inherited from GridCellButton. It overrides DrawButton, where the customized button is drawn using the following code:
ControlPaint.DrawButton(g, rect, buttonState); // Icon rect.Inflate(-3, -3);
Syncfusion.Drawing.BrushPaint.FillRectangle(g, rect, new Syncfusion.Drawing.
BrushInfo(Syncfusion.Drawing.GradientStyle.PathEllipse, Color.Black, Color.Wheat));
After defining MyHeaderCell, it is instantiated in the main form using the following code:
MyHeaderCellModel header = new MyHeaderCellModel(this.gridGroupingControl1.TableModel); this.gridGroupingControl1.TableModel.CellModels["My Column Header"] = header;
ColumnHeaderCellType is initialized with the customized header cell.
this.gridGroupingControl1.Appearance.ColumnHeaderCell.CellType = "My Column Header";
In the TableControlCellClick event, if the clicked cell is a column-header cell, the cell layout is calculated using the following code:
GridCellRendererBase r = e.TableControl.CellRenderers[style.CellType];
r.PerformLayout(e.Inner.RowIndex, e.Inner.ColIndex, style, e.TableControl.RangeInfoToRectangle
(GridRangeInfo.Cell(e.Inner.RowIndex, e.Inner.ColIndex)));
The following code checks whether the pointer is on the cell-button element and if the cell is clickable. If it is true, then it displays a message box with the column name.
if (r.RaiseHitTest(e.Inner.RowIndex, e.Inner.ColIndex, e.Inner.MouseEventArgs, null) == GridHitTestContext.CellButtonElement)
{
string column = (id.Column != null) ? id.Column.ToString() : ""; string s = "Clicked on "
+ id.TableCellType.ToString() + "(" + column + ", " + id.DisplayElement.GetType().
Name + ")";
MessageBox.Show(s); e.Inner.Cancel = true;
}
In the TableControlCellMouseHover event, there is an option for not being GridTableCellType.NestedTableCell, as given below. If the pointer hovers over an area of a nested table, this event will be hit twice: first for the outer table control (which routes the event to the inner table control), then for the inner table control. For the outer table control, the table cell type is GridTableCellType.NestedTableCell.
private void gridGroupingControl1_TableControlCellMouseHover(object sender, GridTableControlCellMouseEventArgs e)
{
GridTableCellStyleInfo style = (GridTableCellStyleInfo) e.TableControl.Model[e.Inner.RowIndex, e.Inner.ColIndex];
GridTableCellStyleInfoIdentity id = style.TableCellIdentity;
if (id.TableCellType != GridTableCellType.NestedTableCell)
{
string column = (id.Column != null) ? id.Column.ToString() : ""; Console.WriteLine("MouseHover over " + id.TableCellType.ToString() +
"(" + column + ", " + id.DisplayElement.GetType().Name + ")");
}
}
In the TableControlCellButtonClicked event, the cell type is checked to expand or collapse the record or group without moving the current record or group.
Record Plus-Minus Cell
The clicked record is expanded if it is collapsed or collapsed if it is
expanded. The event is then cancelled to stop further processing and thereby
stops the IsExpanded call again.
Group Caption Plus-Minus Cell
The group is expanded if it is collapsed or collapsed if it is expanded. The event is then cancelled to stop further processing and thereby stops the IsExpanded call again.
private void gridGroupingControl1_TableControlCellButtonClicked(object sender, GridTableControlCellButtonClickedEventArgs e)
{
GridTableCellStyleInfo style = (GridTableCellStyleInfo) e.TableControl.Model[e.Inner.RowIndex, e.Inner.ColIndex];
GridTableCellStyleInfoIdentity id = style.TableCellIdentity;
if (id.TableCellType != GridTableCellType.NestedTableCell)
{
string column = (id.Column != null) ? id.Column.ToString() : "";
string s = "TableControlCellButtonClicked on " + id.TableCellType.ToString() + "(" + column + ", " + id.DisplayElement.GetType().Name + ")";
Console.WriteLine(s);
e.Inner.Cancel = true;
}
if (id.TableCellType == GridTableCellType.RecordPlusMinusCell)
{
Record r = id.DisplayElement.ParentRecord;
r.IsExpanded = !r.IsExpanded;
e.Inner.Cancel = true;
}
if (id.TableCellType == GridTableCellType.GroupCaptionPlusMinusCell)
{
Group g = id.DisplayElement.ParentGroup;
g.IsExpanded = !g.IsExpanded;
e.Inner.Cancel = true;
}
}