public void AutoSizeGrid(DataGrid dg)
{
int numRows = ((DataTable)dg.DataSource).Rows.Count;
Graphics g = Graphics.FromHwnd(dg.Handle);
StringFormat sf = new StringFormat(StringFormat.GenericTypographic);
SizeF size;
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(dg,null);
ArrayList DataGridRows = new ArrayList();
foreach (object dgrr in dgra)
{
if (dgrr.ToString().EndsWith("DataGridRelationshipRow")==true)
DataGridRows.Add(dgrr);
}
for (int i = 0; i < ??; ++i)
{
size = g.MeasureString(dg[i,1].ToString(),dg.Font,400,sf);
int h = Convert.ToInt32(size.Height);
h = h + 8;
PropertyInfo pi = DataGridRows[i].GetType().GetProperty("Height");
pi.SetValue(DataGridRows[i],0,null);
}
g.Dispose();
}