About DevExpress GridView line head line number and the line number display device according to the dynamic adaptive

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/u012097590/article/details/90898424

GridView row number is not displayed by default, need to draw the line in the event CustomDrawRowIndicator number one, the line numbers display the code as follows

void CustomDrawRowIndicator (object sender, RowIndicatorCustomDrawEventArgs e)
            {
                if (e.RowHandle >= 0)
                {
                    e.Info.DisplayText = (e.RowHandle + 1).ToString();
                }
            }

GridView default line width smaller head, often you need to set GridView.IndicatorWidth property to set the width, but the width is fixed, if the list of data is large, a small set of data behind the line number appears incomplete, if you set a larger , such as a 1w line, then browse the previous data will feel when the display is too wide, does not meet the design beautiful, indeed, awkward, if you can dynamically to determine the width of its first line under the current line number size, then it the perfect. Let me introduce the implementation steps:

GridView provides TopRowChanged event, when the data scrolling when triggered to this event, then we can use this event when scrolling data at this time to calculate the maximum display line numbers, and then to calculate the maximum line number width, the width is calculated as follows:

/// <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;
        }

So TopRowChanged events could write:

 private  void OnTopRowChanged(object sender, EventArgs e)
        {
            GridView view = sender as GridView;
            if (view == null)
                return;
            int width = CalcIndicatorBestWidth(view);
            if ((view.IndicatorWidth - 4 < width || view.IndicatorWidth + 4 > width) && view.IndicatorWidth != width)
            {
                view.IndicatorWidth = width;
            }
        }

In order to facilitate copy paste, see Copy the following code to paste in your project, call in the constructor parameters to pass a gridview:

public static 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.TopRowChanged += (s, e) =>
            {
                int width = CalcIndicatorBestWidth(view);
                if ((view.IndicatorWidth - 4 < width || view.IndicatorWidth + 4 > width) && view.IndicatorWidth != width)
                {
                    view.IndicatorWidth = width;
                }
            };
            
        }
        /// <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;
        }

Renderings:

Download the source code  (source code unnecessary, please feel free Tyrant)

Seeking a reward:

Guess you like

Origin blog.csdn.net/u012097590/article/details/90898424