Class GridUnboundRowCellRenderer
Represents the class that used for drawing the unbound row cells.
Inheritance
Inherited Members
Namespace: Syncfusion.WinForms.DataGrid.Renderers
Assembly: Syncfusion.SfDataGrid.WinForms.dll
Syntax
public class GridUnboundRowCellRenderer : GridTextBoxCellRenderer, IGridCellRenderer<TableControl>, IDisposable
Examples
The following example shows how to replace the custom cell renderer.
// Add the customized unbound row cell render.
sfDataGrid1.UnboundRowCellRenderers.Add("DateTimePicker", new DatePickerRenderer());
// Implementation for custom unbound row cell renderer.
public class DatePickerRenderer : GridVirtualizingCellRendererBase<DateTimePicker>
{
/// <summary>
/// Constructor of the renderer.
/// </summary>
public DatePickerRenderer()
{
}
/// <summary>
/// Edit Element creation.
/// </summary>
/// <returns></returns>
protected override DateTimePicker OnCreateEditUIElement()
{
return new DateTimePicker();
}
#region Edit Element
/// <summary>
/// Initialize the value for edit element.
/// </summary>
/// <param name="dataColumn"></param>
/// <param name="uiElement"></param>
/// <param name="dataContext"></param>
protected override void OnInitializeEditElement(DataColumnBase dataColumn, RowColumnIndex rowColIndex,
DateTimePicker uiElement)
{
SfDataGrid DataGrid = dataColumn.QueryUnboundRowInfoArgs.OriginalSender as SfDataGrid;
DateTime time;
DateTime.TryParse(dataColumn.QueryUnboundRowInfoArgs.Value.ToString(), out time);
(uiElement as DateTimePicker).Value = time;
uiElement.Tag = dataColumn;
Rectangle editorRectangle = this.TableControl.GetCellRectangle(DataGrid.CurrentCell.RowIndex,
DataGrid.CurrentCell.ColumnIndex, true);
var borderWeight = DataGrid.Style.CurrentCellStyle.BorderThickness;
var weight = GetWidthForWeight(borderWeight);
// Adjusts with border thickness for the editing control bounds.
editorRectangle = new Rectangle(editorRectangle.X + weight, editorRectangle.Y + weight, editorRectangle.Width -
(2 * weight), editorRectangle.Height - (2 * weight));
uiElement.Size = editorRectangle.Size;
uiElement.Location = editorRectangle.Location;
uiElement.AutoSize = false;
uiElement.MinimumSize = editorRectangle.Size;
uiElement.Format = DateTimePickerFormat.Long;
this.TableControl.Controls.Add(uiElement);
uiElement.Focus();
}
/// <summary>
/// Occurs when the cell is need to be drawn.
/// </summary>
/// <param name="paint">
/// The <see cref="T:System.Drawing.Graphics"/> that used to draw the cell.
/// </param>
/// <param name="cellRect">
/// The bounds of the cell.
/// </param>
/// <param name="cellValue">
/// The value of the cell.
/// </param>
/// <param name="style">
/// The <see cref="CellStyleInfo"/> of the cell.
/// </param>
/// <param name="column">
/// The <see cref="DataColumnBase"/> of the cell.
/// </param>
/// <param name="rowColumnIndex">The row and column index of the cell.</param>
protected override void OnRender(
Graphics paint,
Rectangle cellRect,
string cellValue,
CellStyleInfo style,
DataColumnBase column,
RowColumnIndex rowColumnIndex)
{
SfDataGrid DataGrid = column.QueryUnboundRowInfoArgs.OriginalSender as SfDataGrid;
bool isEditing = DataGrid.CurrentCell != null && DataGrid.CurrentCell.RowIndex == rowColumnIndex.RowIndex
&& DataGrid.CurrentCell.ColumnIndex == rowColumnIndex.ColumnIndex && DataGrid.CurrentCell.IsEditing;
SolidBrush backColor = new SolidBrush(style.BackColor);
var fillRect = (style.HasBorders) ? cellRect : cellRect.X == 0 ? new Rectangle(cellRect.X, cellRect.Y + 1, cellRect.Width, cellRect.Height - 1) : new Rectangle(cellRect.X + 1, cellRect.Y + 1, cellRect.Width - 1, cellRect.Height - 1);
SolidBrush textColor = new SolidBrush(style.TextColor);
Font font = style.Font != Control.DefaultFont ? style.Font : Control.DefaultFont;
StringFormat format = new StringFormat();
format.LineAlignment = StringAlignment.Center;
if (isEditing)
{
paint.FillRectangle(new SolidBrush(style.BackColor), fillRect);
this.UpdateEditElement(column, this.CurrentCellRendererElement, cellRect);
this.CurrentCellRendererElement.Update();
if (isEditing)
return;
cellValue = this.CurrentCellRendererElement.Text;
textColor = new SolidBrush(style.TextColor);
font = column.GridColumn.CellStyle.Font;
cellRect.X = style.HorizontalAlignment == HorizontalAlignment.Left
? cellRect.X + 2
: column.GridColumn.CellStyle.HorizontalAlignment == HorizontalAlignment.Right
? cellRect.X - 2
: cellRect.X;
}
////Filling BackColor
if (backColor != SystemBrushes.Window)
paint.FillRectangle(backColor, fillRect);
if (!string.IsNullOrEmpty(cellValue))
{
//Draw String
paint.DrawString(cellValue, font, textColor, cellRect, format);
}
format.Dispose();
base.OnRender(paint, cellRect, cellValue, style, column, rowColumnIndex);
}
public override bool CanValidate()
{
return false;
}
/// <summary>
/// Method to wire the Selection Changed event
/// </summary>
/// <param name="uiElement"></param>
protected override void OnWireEditUIElement(DateTimePicker uiElement)
{
base.OnWireEditUIElement(uiElement);
uiElement.ValueChanged += uiElement_ValueChanged;
}
/// <summary>
/// Method to un wire the Selection Changed event.
/// </summary>
/// <param name="uiElement"></param>
protected override void OnUnwireEditUIElement(DateTimePicker uiElement)
{
base.OnUnwireEditUIElement(uiElement);
uiElement.ValueChanged -= uiElement_ValueChanged;
}
void uiElement_ValueChanged(object sender, EventArgs e)
{
var datePicker = sender as DateTimePicker;
if (datePicker.Tag is DataColumn)
{
var dataColumn = datePicker.Tag as DataColumnBase;
dataColumn.QueryUnboundRowInfoArgs.Value = (sender as DateTimePicker).Value;
}
}
/// <summary>
/// Gets the width for the given <see cref="GridBorderWeight"/>.
/// </summary>
/// <param name="weight">
/// The <see cref="GridBorderWeight"/> that needs to be converted to the
/// width.
/// </param>
/// <returns>
/// Returns the border width.
/// </returns>
private static int GetWidthForWeight(GridBorderWeight weight)
{
int width = 1;
switch (weight)
{
case GridBorderWeight.ExtraThin:
width = 1;
break;
case GridBorderWeight.Thin:
width = 1;
break;
case GridBorderWeight.Medium:
width = 2;
break;
case GridBorderWeight.Thick:
width = 3;
break;
case GridBorderWeight.ExtraThick:
width = 4;
break;
case GridBorderWeight.ExtraExtraThick:
width = 4;
break;
}
return width;
}
/// <summary>
/// Update display value and raise event
/// </summary>
/// <param name="dataColumn"></param>
/// <param name="currentRendererElement"></param>
protected override void OnEditingComplete(DataColumnBase dataColumn, DateTimePicker currentRendererElement)
{
dataColumn.QueryUnboundRowInfoArgs.Value = (this.CurrentCellRendererElement as DateTimePicker).Text;
(dataColumn.QueryUnboundRowInfoArgs.OriginalSender as SfDataGrid).RaiseQueryUnboundRowInfo(dataColumn.QueryUnboundRowInfoArgs.GridUnboundRow, dataColumn.QueryUnboundRowInfoArgs.UnboundAction, dataColumn.QueryUnboundRowInfoArgs.Value,
dataColumn.QueryUnboundRowInfoArgs.Column, dataColumn.QueryUnboundRowInfoArgs.CellType, dataColumn.QueryUnboundRowInfoArgs.RowColumnIndex);
base.OnEditingComplete(dataColumn, currentRendererElement);
}
}
The following code shows how to change the renderer of the unbound row cell. The QueryUnboundRowInfo event is used to change the cell type of the unbound row cells. ///
//Raises the QueryUnboundRowInfo event
sfDataGrid1.QueryUnboundRowInfo += dataGrid_QueryUnboundRow;
//In above event, change the cell type for any unbound row.
void dataGrid_QueryUnboundRow(object sender, QueryUnboundRowInfoArgs e)
{
if (e.UnboundAction == UnboundActions.QueryData && e.RowColumnIndex.RowIndex == 3 &&
e.GridUnboundRow.Position == UnboundRowsPosition.Top && e.GridUnboundRow.ShowBelowSummary == true)
{
e.CellType = "DateTimePicker";
e.Value = DateTime.Now;
e.Handled = true;
}
}
Constructors
GridUnboundRowCellRenderer()
Declaration
public GridUnboundRowCellRenderer()
Methods
CanValidate()
Overridden to specify the validation on the numeric filter row cell.
Declaration
public override bool CanValidate()
Returns
Type | Description |
---|---|
System.Boolean | Returns true, when the validation needs to be taken, otherwise return false. |
Overrides
GetControlValue()
Overriden to get the control value.
Declaration
public override object GetControlValue()
Returns
Type | Description |
---|---|
System.Object | Returns the control value. |
Overrides
GetEditorUIElementBounds()
Overriden to get the proper editor element bounds.
Declaration
protected override Rectangle GetEditorUIElementBounds()
Returns
Type | Description |
---|---|
System.Drawing.Rectangle | Returns the editor element bounds. |
Overrides
OnEditingComplete(DataColumnBase, TextBox)
When complete edit, we need to raise query again to provide entered value to customer.
Declaration
protected override void OnEditingComplete(DataColumnBase dataColumn, TextBox currentRendererElement)
Parameters
Type | Name | Description |
---|---|---|
DataColumnBase | dataColumn | DataColumn which holds GridColumn, RowColumnIndex and GridCell |
System.Windows.Forms.TextBox | currentRendererElement | The UIElement that resides in GridUnboundCell |
Overrides
OnInitializeEditElement(DataColumnBase, RowColumnIndex, TextBox)
Overriden to initialize the text box as editor control of the cell.
Declaration
protected override void OnInitializeEditElement(DataColumnBase column, RowColumnIndex rowColumnIndex, TextBox uiElement)
Parameters
Type | Name | Description |
---|---|---|
DataColumnBase | column | The GridColumn of the current cell. |
RowColumnIndex | rowColumnIndex | The row and column index of the current cell. |
System.Windows.Forms.TextBox | uiElement | The text box of the current cell. |
Overrides
OnKeyDown(DataColumnBase, RowColumnIndex, KeyEventArgs)
Occurs when the key is pressed while the cell has focus.
Declaration
protected override void OnKeyDown(DataColumnBase dataColumn, RowColumnIndex rowColumnIndex, KeyEventArgs e)
Parameters
Type | Name | Description |
---|---|---|
DataColumnBase | dataColumn | The DataColumnBase of the cell. |
RowColumnIndex | rowColumnIndex | The row and column index of the cell. |
System.Windows.Forms.KeyEventArgs | e | The System.Windows.Forms.KeyEventArgs that contains the event data. |
Overrides
OnMouseDown(DataColumnBase, RowColumnIndex, MouseEventArgs)
Occurs when mouse down on the check box cell.
Declaration
protected override void OnMouseDown(DataColumnBase dataColumn, RowColumnIndex rowColumnIndex, MouseEventArgs e)
Parameters
Type | Name | Description |
---|---|---|
DataColumnBase | dataColumn | The data column of the check box cell. |
RowColumnIndex | rowColumnIndex | The row and column index of the check box cell. |
System.Windows.Forms.MouseEventArgs | e | The MouseEventArgs that contains the event data. |
Overrides
OnMouseHoverLeave(DataColumnBase, RowColumnIndex, MouseEventArgs)
Occurs when the mouse leaves the cell.
Declaration
protected override void OnMouseHoverLeave(DataColumnBase dataColumn, RowColumnIndex rowColumnIndex, MouseEventArgs e)
Parameters
Type | Name | Description |
---|---|---|
DataColumnBase | dataColumn | The DataColumnBase of the cell. |
RowColumnIndex | rowColumnIndex | RowColumnIndex of the cell from which the mouse leaves. |
System.Windows.Forms.MouseEventArgs | e | The System.Windows.Forms.MouseEventArgs contains the event data. |
Overrides
OnMouseMove(DataColumnBase, RowColumnIndex, MouseEventArgs)
Occurs when the mouse pointer rests on the cell.
Declaration
protected override void OnMouseMove(DataColumnBase dataColumn, RowColumnIndex rowColumnIndex, MouseEventArgs e)
Parameters
Type | Name | Description |
---|---|---|
DataColumnBase | dataColumn | The DataColumnBase of the cell. |
RowColumnIndex | rowColumnIndex | The row and column index of the cell. |
System.Windows.Forms.MouseEventArgs | e | The System.Windows.Forms.MouseEventArgs contains the event data. |
Overrides
OnRender(Graphics, Rectangle, String, CellStyleInfo, DataColumnBase, RowColumnIndex)
Overridden to draw the text box cell.
Declaration
protected override void OnRender(Graphics paint, Rectangle cellRect, string cellValue, CellStyleInfo style, DataColumnBase column, RowColumnIndex rowColumnIndex)
Parameters
Type | Name | Description |
---|---|---|
System.Drawing.Graphics | paint | The System.Drawing.Graphics that used to draw the cell. |
System.Drawing.Rectangle | cellRect | The bounds of the cell. |
System.String | cellValue | The value of the cell. |
CellStyleInfo | style | The CellStyleInfo of the cell. |
DataColumnBase | column | The DataColumnBase of the cell. |
RowColumnIndex | rowColumnIndex | The row and column index of the cell. |
Overrides
SetControlValue(Object)
Overriden to set the control value.
Declaration
public override void SetControlValue(object value)
Parameters
Type | Name | Description |
---|---|---|
System.Object | value | The control value. |