我用了这个http://www.devexpress.com/Support/Center/p/Q233111.aspx (代码在 VB 中,所以我将其转换为 C#)以获取垂直列标题。我得到了垂直标题,但我的问题是其中一些不合适,所以它们不完全可见。
是否可以自动调整列标题的高度? (所有高度设置为最大高度)
最佳答案
如图Devexpress support center我认为这将是您问题的解决方案 首先向您的解决方案添加一个帮助程序类
public class AutoHeightHelper
{
GridView view;
public AutoHeightHelper(GridView view)
{
this.view = view;
EnableColumnPanelAutoHeight();
}
public void EnableColumnPanelAutoHeight()
{
SetColumnPanelHeight();
SubscribeToEvents();
}
private void SubscribeToEvents()
{
view.ColumnWidthChanged += OnColumnWidthChanged;
view.GridControl.Resize += OnGridControlResize;
view.EndSorting += OnGridColumnEndSorting;
}
void OnGridColumnEndSorting(object sender, EventArgs e)
{
view.GridControl.BeginInvoke(new MethodInvoker(SetColumnPanelHeight));
}
void OnGridControlResize(object sender, EventArgs e)
{
SetColumnPanelHeight();
}
void OnColumnWidthChanged(object sender, DevExpress.XtraGrid.Views.Base.ColumnEventArgs e)
{
SetColumnPanelHeight();
}
private void SetColumnPanelHeight()
{
GridViewInfo viewInfo = view.GetViewInfo() as GridViewInfo;
int height = 0;
for (int i = 0; i < view.VisibleColumns.Count; i++)
height = Math.Max(GetColumnBestHeight(viewInfo, view.VisibleColumns[i]), height);
view.ColumnPanelRowHeight = height;
}
private int GetColumnBestHeight(GridViewInfo viewInfo, GridColumn column)
{
GridColumnInfoArgs ex = viewInfo.ColumnsInfo[column];
GraphicsInfo grInfo = new GraphicsInfo();
grInfo.AddGraphics(null);
ex.Cache = grInfo.Cache;
bool canDrawMore = true;
Size captionSize = CalcCaptionTextSize(grInfo.Cache, ex as HeaderObjectInfoArgs, column.GetCaption());
Size res = ex.InnerElements.CalcMinSize(grInfo.Graphics, ref canDrawMore);
res.Height = Math.Max(res.Height, captionSize.Height);
res.Width += captionSize.Width;
res = viewInfo.Painter.ElementsPainter.Column.CalcBoundsByClientRectangle(ex, new Rectangle(Point.Empty, res)).Size;
grInfo.ReleaseGraphics();
return res.Height;
}
Size CalcCaptionTextSize(GraphicsCache cache, HeaderObjectInfoArgs ee, string caption)
{
Size captionSize = ee.Appearance.CalcTextSize(cache, caption, ee.CaptionRect.Width).ToSize();
captionSize.Height++; captionSize.Width++;
return captionSize;
}
public void DisableColumnPanelAutoHeight()
{
UnsubscribeFromEvents();
}
private void UnsubscribeFromEvents()
{
view.ColumnWidthChanged -= OnColumnWidthChanged;
view.GridControl.Resize -= OnGridControlResize;
view.EndSorting -= OnGridColumnEndSorting;
}
}
然后在您的表单上,您应该通过添加以下代码行来使帮助程序类处理 GridView 的列调整大小事件
AutoHeightHelper helper;
private void OnFormLoad(object sender, EventArgs e)
{
helper = new AutoHeightHelper(gridView1);
helper.EnableColumnPanelAutoHeight();
}
private void OnFormClosing(object sender, FormClosingEventArgs e)
{
helper.DisableColumnPanelAutoHeight();
}
希望这有助于...
关于c# - 使用 DeveXpress 在 C# 中自动设置垂直列标题的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7558215/