This is my implementation. There seems to be a few problems with wrapping but.
public class MemoCell: DataGridViewTextBoxCell
{
public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{
dataGridViewCellStyle.WrapMode = DataGridViewTriState.True;
base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
}
public override void PositionEditingControl(bool setLocation, bool setSize, Rectangle cellBounds, Rectangle cellClip, DataGridViewCellStyle cellStyle, bool singleVerticalBorderAdded, bool singleHorizontalBorderAdded, bool isFirstDisplayedColumn, bool isFirstDisplayedRow)
{
base.PositionEditingControl(setLocation, setSize, cellBounds, cellClip, cellStyle, singleVerticalBorderAdded, singleHorizontalBorderAdded, isFirstDisplayedColumn, isFirstDisplayedRow);
}
public override Rectangle PositionEditingPanel(Rectangle cellBounds, Rectangle cellClip, DataGridViewCellStyle cellStyle, bool singleVerticalBorderAdded, bool singleHorizontalBorderAdded, bool isFirstDisplayedColumn, bool isFirstDisplayedRow)
{
string value = (string)this.EditedFormattedValue;
Size maxSize = MaxSize(cellBounds.Location);
Size stringSize = MeasureString(value, cellStyle.Font, new SizeF((float)maxSize.Width, (float)maxSize.Height));
Console.WriteLine(stringSize);
Rectangle maxRectangle = new Rectangle(cellBounds.Location, MaxSize(cellBounds.Location));
int finalWidth = Math.Max(stringSize.Width, cellBounds.Width);
int finalHeight = Math.Max(stringSize.Height, cellBounds.Height);
Rectangle finalRectangle = new Rectangle(cellBounds.Location, new Size(finalWidth, finalHeight));
return base.PositionEditingPanel(finalRectangle, finalRectangle, cellStyle, singleVerticalBorderAdded, singleHorizontalBorderAdded, isFirstDisplayedColumn, isFirstDisplayedRow);
}
public override Type EditType
{
get
{
return typeof(MemoTextBox);
}
}
public Size MeasureString(string value, Font font, SizeF layoutArea)
{
int characters,lines;
int lineHeight;
Graphics g = Graphics.FromHwnd(this.DataGridView.Handle);
lineHeight = g.MeasureString("MeasureString", font).ToSize().Height;
Size size = g.MeasureString(value, font, layoutArea, new StringFormat(StringFormatFlags.MeasureTrailingSpaces), out characters, out lines).ToSize();
Console.WriteLine("Characters{0}, lines{1}", characters, lines);
if (value.EndsWith(Environment.NewLine))
{
lines += 1;
}
size.Height = (lineHeight * lines);
return size;
}
private Size MaxSize(Point relativeLocation)
{
Size gridSize = this.DataGridView.Size;
int widthLeftover = gridSize.Width - relativeLocation.X;
int heightLeftover = gridSize.Height - relativeLocation.Y;
if (widthLeftover < 0
|| heightLeftover < 0)
{
throw new Exception("Tp can't be right!");
}
return new Size(widthLeftover, heightLeftover);
}
}