Devexpress,行号显示
private void GridView1_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
{
try
{
e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
if (e.Info.IsRowIndicator)
{
if (e.RowHandle < 0) return;
e.Info.DisplayText = (e.RowHandle + 1).ToString();
}
}
catch (Exception exception)
{
LogHelper.Error("gridview显示行号异常", exception);
}
}
private void GridView1_TopRowChanged(object sender, EventArgs e)
{
try
{
if (!(sender is GridView view)) return;
int width = CalcIndicatorWidth(view);
if ((view.IndicatorWidth - 4 < width) || view.IndicatorWidth + 4 > width && view.IndicatorWidth != width)
{
view.IndicatorWidth = width;
}
}
catch (Exception exception)
{
LogHelper.Error("行号行款修改异常", exception);
}
}
private int CalcIndicatorWidth(GridView view)
{
Graphics graphics = new Control().CreateGraphics();
int count = view.TopRowIndex + ((DevExpress.XtraGrid.Views.Grid.ViewInfo.GridViewInfo)view.GetViewInfo()).RowsInfo.Count;
count = count == 0 ? 30 : count;
SizeF sizeF = graphics.MeasureString(count.ToString(), view.Appearance.Row.Font);
return Convert.ToInt32(sizeF.Width) + 40;
}