Form code:
There is a standard SyncFusion gridControl on the form.
private void Form1_Load(object sender, EventArgs e)
{
if (!gridControl1.CellModels.ContainsKey("rText"))
gridControl1.CellModels.Add("rText", new rTextCellModel(gridControl1.Model));
gridControl1.RowStyles[1].CellType = "rText";
}
rText class code:
#region rText
///
///
public partial class rText : System.Windows.Forms.TextBox //, IDataGridViewEditingControl
{
private string m_MyVal = "";
#region Constructors
///
/// Default constructor.
///
public rText()
{
this.Validating -= new CancelEventHandler(rText_Validating);
this.Validating += new CancelEventHandler(rText_Validating);
}
void rText_Validating(object sender, CancelEventArgs e)
{
System.Console.WriteLine("Validating");
} //End of the rText() constructor.
public void Init()
{
this.Text = "12";
}
public string MyVal
{
get
{
return m_MyVal;
}
set
{
m_MyVal = value;
}
}
#endregion
} //End of rText class
#endregion rText
#region rTextCellModel
///
/// Implements the data / model part for n rText cell.
///
///
/// You typically access cell models through the
/// property of the class.
///
[Serializable]
public class rTextCellModel : GridGenericControlCellModel
{
///
/// Initializes a new object.
///
///
/// Initializes a new object
/// and stores a reference to the this cell belongs to.
///
///
The
for this cell model.
///
/// You typically access cell models through the
/// property of the class.
///
public rTextCellModel(GridModel grid) : base(grid)
{
}
///
/// Initializes a new from a serialization stream.
///
///
An object that holds all the data needed to serialize or deserialize this instance.
///
Describes the source and destination of the serialized stream specified by info.
protected rTextCellModel(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
///
public override GridCellRendererBase CreateRenderer(GridControlBase control)
{
return new rTextCellRenderer(control, this);
}
static Size m_rTextControlSize = Size.Empty;
///
/// Gets and Sets the rText control size.
///
///
Size object
public static Size rTextControlSize
{
get
{
if (m_rTextControlSize.IsEmpty)
{
//determine default size of rText when shown in a form
TopLevelWindow f = new TopLevelWindow();
f.Location = new Point(10000, 10000);
f.Size = new Size(500, 500);
f.ShowWindowTopMost();
rText drawrText = new rText();
f.Controls.Add(drawrText);
f.Visible = true;
m_rTextControlSize = drawrText.Size;
}
return m_rTextControlSize;
}
}
///
protected override Size OnQueryPrefferedClientSize(Graphics g, int rowIndex, int colIndex, GridStyleInfo style, GridQueryBounds queryBounds)
{
Size size = rTextControlSize;
size.Width += 8;
size.Height += 8;
return size;
}
///
/// Sets the default text in the rText box from a GridStyleInfo object.
///
///
An rText
///
A GridStyleInfo object for the current cell
public static void InitalizerText(rText t, GridStyleInfo style)
{
InitalizerText(t, style.CellValue);
}
///
/// Sets the default text in the rText box from an object that returns the text in the ToString() method.
///
///
An rText
///
An object that returns the text in the ToString() method
public static void InitalizerText(rText t, object controlValue)
{
t.Text = controlValue.ToString();
}
}
#endregion rTextCellModel
#region rTextCellRenderer
///
/// Implements the renderer part of a rich text cell.
///
public class rTextCellRenderer : GridGenericControlCellRenderer
{
rText drawrText = new rText();
rText editrText = new rText();
rTextCellModel cm;
///
/// Initializes a new rTextCellRenderer object for the given GridControlBase
/// and GridCellModelBase.
///
///
The
that displays this cell renderer.
///
The
that holds data for this cell renderer that should
/// be shared among views.
///
References to GridControlBase
/// and GridCellModelBase will be saved.
public rTextCellRenderer(GridControlBase grid, GridCellModelBase cellModel) : base(grid, cellModel)
{
cm = (rTextCellModel)cellModel;
SupportsFocusControl = true;
FixControlParent(drawrText);
SetControl(editrText);
editrText.TextChanged += new EventHandler(editrText_TextChanged);
}
///
protected override void Dispose(bool disposing)
{
if (disposing)
{
editrText.TextChanged -= new EventHandler(editrText_TextChanged);
editrText.Dispose();
editrText = null;
drawrText.Dispose();
drawrText = null;
cm = null;
}
base.Dispose(disposing);
}
// protected /*internal*/ override bool OnSaveChanges()
///
/// Overridden OnSaveChanges method.
///
///
Boolean, true if successful.
protected override bool OnSaveChanges()
{
this.Grid.Model[RowIndex, ColIndex].CellValue = editrText.Text;
return true;
}
///
protected override void OnActivated()
{
// works around some interaction problem with PropertyGrid specific to this sample;
// force redrawing of rText after property grid was refreshed
Timer t = new Timer();
t.Tick += new EventHandler(t_Tick);
t.Interval = 200;
t.Start();
base.OnActivated();
}
private void t_Tick(object sender, EventArgs e)
{
Timer t = (Timer)sender;
t.Tick += new EventHandler(t_Tick);
t.Dispose();
//this.grid.InvalidateRange(gridRangeInfo.cell(RowIndex, ColIndex));
editrText.Refresh();
}
///
protected override void OnDraw(Graphics g, Rectangle clientRectangle, int rowIndex, int colIndex, GridStyleInfo style)
{
//clientRectangle.Size = GridUtil.Max(clientRectangle.Size, rTextCellModel.rTextControlSize);
if (this.ShouldDrawFocused(rowIndex, colIndex))
{
style.Control = editrText;
editrText.BackColor = style.BackColor;
editrText.BorderStyle = BorderStyle.None;
editrText.Size = clientRectangle.Size;
// editrText.ForeColor = style.Interior.ForeColor;
}
else
{
style.Control = drawrText;
drawrText.BackColor = style.BackColor;
drawrText.BorderStyle = BorderStyle.None;
// drawrText.ForeColor = style.Interior.ForeColor;
drawrText.Size = clientRectangle.Size;
rTextCellModel.InitalizerText(drawrText, style);
}
base.OnDraw(g, clientRectangle, rowIndex, colIndex, style);
}
///
protected override void InitializeControlText(object controlValue)
{
rTextCellModel.InitalizerText(this.editrText, controlValue);
}
///
protected override Rectangle OnLayout(int rowIndex, int colIndex, GridStyleInfo style, Rectangle innerBounds, Rectangle[] buttonsBounds)
{
//Rectangle r = base.OnLayout(rowIndex, colIndex, style, innerBounds, buttonsBounds);
//r.Size = innerBounds.Size;
Rectangle r = innerBounds;
//r.Inflate(-1, -1);
return r;
}
private void editrText_TextChanged(object sender, EventArgs e)
{
//raise CurrentCellChanging event and set CurrentCell.Modified = true
if (base.NotifyCurrentCellChanging())
{
ControlValue = editrText.Text;
base.NotifyCurrentCellChanged();
}
}
}
#endregion rTextCellRenderer