资源简介

资源的相关博文链接 https://blog.csdn.net/u012097590/article/details/90898424 DevExpress的版本用的是17.1,资源内部版本转换的方法

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BestFitGridViewIndicatorWidth
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            BindCustomDrawRowIndicator(gridView1);
        }
        DataTable CreateDt(int rowCount)
        {
            var dt = new DataTable();
            for (int i = 0; i < 3; i++)
            {
                dt.Columns.Add(“col“ + i);
            }
            for (int i = 0; i < rowCount; i++)
            {
                var dr = dt.NewRow();
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    dr[j] = $“col{i}{j}“;
                }
                dt.Rows.Add(dr);
            }
            return dt;
        }
        private void Form1_Load(object sender EventArgs e)
        {
            var dt = CreateDt(99999);
            gridControl1.DataSource = dt;
        }
        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;
                }
            };
            
        }
        /// 
        /// 计算行头宽度
        /// 

        /// 
        /// 
        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;
        }
        /// 
        /// 计算默认的宽度
        /// 

        /// 
        /// 
        int CalcIndicatorDefaultWidth(DevExpress.XtraGrid.Views.Grid.GridView view)
        {
            var grid = view.GridControl;
            Graphics graphics = new Control().CreateGraphics();
            SizeF sizeF = new SizeF();
        

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2019-06-05 11:01  BestFitGridViewIndicatorWidth\
     目录           0  2019-06-05 09:30  BestFitGridViewIndicatorWidth\.vs\
     目录           0  2019-06-05 09:30  BestFitGridViewIndicatorWidth\.vs\BestFitGridViewIndicatorWidth\
     目录           0  2019-06-05 09:30  BestFitGridViewIndicatorWidth\.vs\BestFitGridViewIndicatorWidth\v14\
     文件       17408  2019-06-05 10:57  BestFitGridViewIndicatorWidth\.vs\BestFitGridViewIndicatorWidth\v14\.suo
     目录           0  2019-06-05 10:38  BestFitGridViewIndicatorWidth\BestFitGridViewIndicatorWidth\
     文件        4729  2019-06-05 10:22  BestFitGridViewIndicatorWidth\BestFitGridViewIndicatorWidth\BestFitGridViewIndicatorWidth.csproj
     文件        2981  2019-06-05 10:38  BestFitGridViewIndicatorWidth\BestFitGridViewIndicatorWidth\Form1.Designer.cs
     文件        3426  2019-06-05 10:38  BestFitGridViewIndicatorWidth\BestFitGridViewIndicatorWidth\Form1.cs
     文件        5817  2019-06-05 10:38  BestFitGridViewIndicatorWidth\BestFitGridViewIndicatorWidth\Form1.resx
     文件         510  2019-06-05 09:30  BestFitGridViewIndicatorWidth\BestFitGridViewIndicatorWidth\Program.cs
     目录           0  2019-06-05 10:57  BestFitGridViewIndicatorWidth\BestFitGridViewIndicatorWidth\Properties\
     文件        1384  2019-06-05 09:30  BestFitGridViewIndicatorWidth\BestFitGridViewIndicatorWidth\Properties\AssemblyInfo.cs
     文件        2871  2019-06-05 09:30  BestFitGridViewIndicatorWidth\BestFitGridViewIndicatorWidth\Properties\Resources.Designer.cs
     文件        5612  2019-06-05 09:30  BestFitGridViewIndicatorWidth\BestFitGridViewIndicatorWidth\Properties\Resources.resx
     文件        1116  2019-06-05 09:30  BestFitGridViewIndicatorWidth\BestFitGridViewIndicatorWidth\Properties\Settings.Designer.cs
     文件         249  2019-06-05 09:30  BestFitGridViewIndicatorWidth\BestFitGridViewIndicatorWidth\Properties\Settings.settings
     文件         252  2019-06-05 10:57  BestFitGridViewIndicatorWidth\BestFitGridViewIndicatorWidth\Properties\licenses.licx
     目录           0  2019-06-05 09:30  BestFitGridViewIndicatorWidth\BestFitGridViewIndicatorWidth\bin\
     目录           0  2019-06-05 10:22  BestFitGridViewIndicatorWidth\BestFitGridViewIndicatorWidth\bin\Debug\
     文件       12800  2019-06-05 10:57  BestFitGridViewIndicatorWidth\BestFitGridViewIndicatorWidth\bin\Debug\BestFitGridViewIndicatorWidth.exe
     文件       24064  2019-06-05 10:57  BestFitGridViewIndicatorWidth\BestFitGridViewIndicatorWidth\bin\Debug\BestFitGridViewIndicatorWidth.pdb
     文件       22688  2019-06-05 11:01  BestFitGridViewIndicatorWidth\BestFitGridViewIndicatorWidth\bin\Debug\BestFitGridViewIndicatorWidth.vshost.exe
     文件         490  2017-03-19 05:00  BestFitGridViewIndicatorWidth\BestFitGridViewIndicatorWidth\bin\Debug\BestFitGridViewIndicatorWidth.vshost.exe.manifest
     文件     6234352  2018-04-28 13:37  BestFitGridViewIndicatorWidth\BestFitGridViewIndicatorWidth\bin\Debug\DevExpress.Data.v17.1.dll
     文件     1330793  2018-04-28 13:37  BestFitGridViewIndicatorWidth\BestFitGridViewIndicatorWidth\bin\Debug\DevExpress.Data.v17.1.xml
     文件     2056432  2018-04-28 13:37  BestFitGridViewIndicatorWidth\BestFitGridViewIndicatorWidth\bin\Debug\DevExpress.Office.v17.1.Core.dll
     文件      100312  2018-04-28 13:37  BestFitGridViewIndicatorWidth\BestFitGridViewIndicatorWidth\bin\Debug\DevExpress.Office.v17.1.Core.xml
     文件     1820400  2018-04-28 13:37  BestFitGridViewIndicatorWidth\BestFitGridViewIndicatorWidth\bin\Debug\DevExpress.Pdf.v17.1.Core.dll
     文件      348857  2018-04-28 13:37  BestFitGridViewIndicatorWidth\BestFitGridViewIndicatorWidth\bin\Debug\DevExpress.Pdf.v17.1.Core.xml
     文件     4197616  2018-04-28 13:37  BestFitGridViewIndicatorWidth\BestFitGridViewIndicatorWidth\bin\Debug\DevExpress.Printing.v17.1.Core.dll
............此处省略90个文件信息

评论

共有 条评论