在Override上设置RowSizingAutoMaxLines会将其设置为所有行。您可以做的是计算必要的行高并将其设置为当前行,假设您已预先将RowSizing设置为Free或AutoFree。您可以使用图形MeasureString来计算一行的高度和设置,则每行的高度是这样的:
首次设置网格:
private void UltraGrid1_InitializeLayout(object sender, InitializeLayoutEventArgs e)
{
// I think you need row selectors as you set their style
e.Layout.Override.RowSelectors = Infragistics.Win.DefaultableBoolean.True;
// Set the RowSizing to some Free value to allow each row to has its onw height
e.Layout.Override.RowSizing = RowSizing.AutoFree;
// I think you have multiline text in the cells, so you should set CellMultiLine to true too
e.Layout.Override.CellMultiLine = Infragistics.Win.DefaultableBoolean.True;
}
然后测量一行,并设置该行的高度:
// Calculate the height of one line of text
var oneLineHeight = float.MinValue;
using(Graphics g = this.ultraGrid1.CreateGraphics())
{
oneLineHeight = g.MeasureString("Jj", this.ultraGrid1.Font, int.MaxValue, StringFormat.GenericTypographic).Height;
}
// Set the row selectors' style and the row's height
if(e.Cell.Column.Layout.Override.RowSelectorStyle == Infragistics.Win.HeaderStyle.Default)
{
e.Cell.Column.Layout.Override.RowSelectorStyle = Infragistics.Win.HeaderStyle.XPThemed;
// Add 4 to add some padding
e.Cell.Row.Height = (int)(oneLineHeight * 20 + 4);
}
else
{
e.Cell.Column.Layout.Override.RowSelectorStyle = Infragistics.Win.HeaderStyle.Default;
// Add 4 to add some padding
e.Cell.Row.Height = (int)(oneLineHeight * 4 + 4);
}
感谢@wnvko,这是解决问题的好方法。 –