admin 管理员组

文章数量: 887021

GridView

1.显示序号(通用)

        #region  序号public void BindCustomDrawRowIndicator(DevExpress.XtraGrid.Views.Grid.GridView view){view.IndicatorWidth = CalcIndicatorDefaultWidth(view);view.CustomDrawRowIndicator += (s, e) =>{if (e.RowHandle >= 0){e.Info.DisplayText = (e.RowHandle + 1).ToString();view.Appearance.FocusedRow.BackColor = Color.Empty;view.Appearance.SelectedRow.BackColor = Color.Empty;//view.OptionsSelection.EnableAppearanceFocusedCell = true;//view.OptionsSelection.EnableAppearanceFocusedRow = true;}};view.TopRowChanged += (s, e) =>{int width = CalcIndicatorBestWidth(view);if ((view.IndicatorWidth - 4 < width || view.IndicatorWidth + 4 > width) && view.IndicatorWidth != width){view.IndicatorWidth = width;view.Appearance.FocusedRow.BackColor = Color.Empty;view.Appearance.SelectedRow.BackColor = Color.Empty;//view.OptionsSelection.EnableAppearanceFocusedCell = true;//view.OptionsSelection.EnableAppearanceFocusedRow = true;}};}/// <summary>/// 计算行头宽度/// </summary>/// <param name="sender"></param>/// <returns></returns>int CalcIndicatorBestWidth(DevExpress.XtraGrid.Views.Grid.GridView view){Graphics graphics = new Control().CreateGraphics();SizeF sizeF = new SizeF();int count = view.TopRowIndex + ((DevExpress.XtraGrid.Views.Grid.ViewInfo.GridViewInfo)view.GetViewInfo()).RowsInfo.Count;if (count == 0){count = 30;}sizeF = graphics.MeasureString(count.ToString(), view.Appearance.Row.Font);return Convert.ToInt32(sizeF.Width) + 20;}/// <summary>/// 计算默认的宽度/// </summary>/// <param name="view"></param>/// <returns></returns>int CalcIndicatorDefaultWidth(DevExpress.XtraGrid.Views.Grid.GridView view){var grid = view.GridControl;Graphics graphics = new Control().CreateGraphics();SizeF sizeF = new SizeF();int rowHeight = 22;//22是Row的估计高度if (view.RowHeight > 0){rowHeight = view.RowHeight;}int count = grid != null ? grid.Height / rowHeight : 30;sizeF = graphics.MeasureString(count.ToString(), view.Appearance.Row.Font);return Convert.ToInt32(sizeF.Width) + 20;}#endregion

参考
关于DevExpress GridView行头显示行号且根据行号宽度动态自适应显示_涛神-Dev 的博客-CSDN博客_devexpress 行号

调用

2.显示序号

在MainView中添加一个CustomDrawRowIndicator事件,在事件处理器中添加以下代码

if (e.Info.IsRowIndicator && e.RowHandle >= 0) 

                              e.Info.DisplayText = e.RowHandle.ToString(); 

}
修改InDicatorWidth属性   可以修改其宽度

3.是否显示

本文标签: GridView