I have tried everything I know to get a textbox in a DataGridTextBoxColumn to word wrap, including setting wordwrap, multiline, alignment and scrollbars. Nothing works, I must be missing something.
if (!bPainted) { this.dg = this.DataGridTableStyle.DataGrid; this.getHeightList(); }
// clear the cell g.FillRectangle(backBrush, bounds);
// draw the value String s = this.GetColumnValueAtRow(source, rowNum).ToString(); Rectangle r = new Rectangle(bounds.X, bounds.Y, bounds.Width, bounds.Height);
r.Inflate(0, -1);
// determine the appropriate column height SizeF sDraw = g.MeasureString(s, this.TextBox.Font, this.Width, mDrawText); int h = (int)(sDraw.Height + 5);
if (mbAdjustHeight) { try { PropertyInfo propInfo = arrHeights[rowNum].GetType().GetProperty("Height");
// get the current height int currentHeight = (int)propInfo.GetValue(arrHeights[rowNum], null);
// adjust the height if (h>currentHeight) { propInfo.SetValue(arrHeights[rowNum], h, null); Size size = dg.Size; dg.Size = new Size(size.Width, size.Height - 10); dg.Size = size; } } catch (Exception ex) { // something went wrong, restore the default height this.getHeightList(); }
Datagrid
Dreedle
i want to use the code in my appl.do you have the latest with all fixes,kindly mail me at materialvbdotnet@yahoo.com
thanks in adv.
shashi
Somebody
Epicycles
As with just about anything to do with the datagrid, you have to do some subclassing to make it work.
In this case, you need to subclass the DataGridTextBoxColumn.
Here's one that I used in a project recently:
/*
*
* Concept and much of this code courtesy of: Ken Tucker (Microsoft MVP / VIP)
*
*/
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Reflection;
using System.Collections;
namespace Wcsg.UI.Windows
{
/// <summary>
/// Datagrid column that displays text in a multiline, word-wrapped textbox
/// </summary>
public class DataGridMultiLineTextBox : DataGridTextBoxColumn
{
private HorizontalAlignment dataAlignment;
private StringFormat mDrawText = new StringFormat();
private bool mbAdjustHeight;
private ArrayList arrHeights;
private DataGrid dg;
public HorizontalAlignment DataAlignment
{
get
{
return dataAlignment;
}
set
{
dataAlignment = value;
switch (dataAlignment)
{
case HorizontalAlignment.Center:
mDrawText.Alignment = StringAlignment.Center;
break;
case HorizontalAlignment.Right:
mDrawText.Alignment = StringAlignment.Far;
break;
case HorizontalAlignment.Left:
mDrawText.Alignment = StringAlignment.Near;
break;
}
}
}
public bool AutoAdjustHeight
{
get
{
return mbAdjustHeight;
}
set
{
mbAdjustHeight = value;
try
{
dg.Invalidate();
}
catch (Exception ex)
{
// do nothing
}
}
}
public DataGridMultiLineTextBox()
{
dataAlignment = HorizontalAlignment.Left;
mDrawText.Alignment = StringAlignment.Near;
this.ReadOnly = true;
}
private void getHeightList()
{
MethodInfo mi = dg.GetType().GetMethod("get_DataGridRows", BindingFlags.FlattenHierarchy |
BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.NonPublic |
BindingFlags.Public | BindingFlags.Static);
System.Array dgra = (System.Array)(mi.Invoke(this.dg, null));
arrHeights = new ArrayList();
foreach (Object dgRowHeight in dgra)
{
if (dgRowHeight.ToString().EndsWith("DataGridRelationshipRow"))
{
arrHeights.Add(dgRowHeight);
}
}
}
protected override void Edit(CurrencyManager source, int rowNum, Rectangle bounds,
bool readOnly, String instantText, bool cellIsVisible)
{
base.Edit (source, rowNum, bounds, readOnly, instantText, cellIsVisible);
this.TextBox.TextAlign = this.dataAlignment;
this.TextBox.Multiline = this.mbAdjustHeight;
if (rowNum == source.Count - 1)
{
this.getHeightList();
}
}
protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source,
int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight)
{
bool bPainted = false;
if (!bPainted)
{
this.dg = this.DataGridTableStyle.DataGrid;
this.getHeightList();
}
// clear the cell
g.FillRectangle(backBrush, bounds);
// draw the value
String s = this.GetColumnValueAtRow(source, rowNum).ToString();
Rectangle r = new Rectangle(bounds.X, bounds.Y, bounds.Width, bounds.Height);
r.Inflate(0, -1);
// determine the appropriate column height
SizeF sDraw = g.MeasureString(s, this.TextBox.Font, this.Width, mDrawText);
int h = (int)(sDraw.Height + 5);
if (mbAdjustHeight)
{
try
{
PropertyInfo propInfo = arrHeights[rowNum].GetType().GetProperty("Height");
// get the current height
int currentHeight = (int)propInfo.GetValue(arrHeights[rowNum], null);
// adjust the height
if (h>currentHeight)
{
propInfo.SetValue(arrHeights[rowNum], h, null);
Size size = dg.Size;
dg.Size = new Size(size.Width, size.Height - 10);
dg.Size = size;
}
}
catch (Exception ex)
{
// something went wrong, restore the default height
this.getHeightList();
}
g.DrawString(s, base.TextBox.Font, foreBrush, r, mDrawText);
bPainted = true;
}
}
}
}
Sevana Oy