首页 > 其他分享 >winform dataGridView MultipleLayeredColumnHeader

winform dataGridView MultipleLayeredColumnHeader

时间:2023-05-31 13:58:02浏览次数:32  
标签:MultipleLayeredColumnHeader r1 dataGridView Add dataGridView1 new Height Columns

  private void MainForm_Load(object sender, EventArgs e)
        {
            this.dataGridView1.Columns.Add("JanWin", "Win");
            this.dataGridView1.Columns.Add("JanLoss", "Loss");
            this.dataGridView1.Columns.Add("FebWin", "Win");
            this.dataGridView1.Columns.Add("FebLoss", "Loss");
            this.dataGridView1.Columns.Add("MarWin", "Win");
            this.dataGridView1.Columns.Add("MarLoss", "Loss");

            for (int j = 0; j < this.dataGridView1.ColumnCount; j++)
            {
                this.dataGridView1.Columns[j].Width = 45;
            }

            // Enable resizing on the column headers;
            this.dataGridView1.ColumnHeadersHeightSizeMode =
                 DataGridViewColumnHeadersHeightSizeMode.EnableResizing;

            // Adjust the height for the column headers
            this.dataGridView1.ColumnHeadersHeight =
                        this.dataGridView1.ColumnHeadersHeight * 2;

            // Adjust the text alignment on the column headers to make the text display
            // at the center of the bottom;
            this.dataGridView1.ColumnHeadersDefaultCellStyle.Alignment =
                 DataGridViewContentAlignment.BottomCenter;

            // Handle the CellPainting event to draw text for each header cell
            this.dataGridView1.CellPainting += new
                 DataGridViewCellPaintingEventHandler(dataGridView1_CellPainting);

            // Handle the Paint event to draw "merged" header cells;
            this.dataGridView1.Paint += new PaintEventHandler(dataGridView1_Paint);
        }

        void dataGridView1_Paint(object sender, PaintEventArgs e)
        {
            // Data for the "merged" header cells
            string[] monthes = { "January", "February", "March" };

            for (int j = 0; j < this.dataGridView1.ColumnCount;)
            {
                // Get the column header cell bounds
                Rectangle r1 = this.dataGridView1.GetCellDisplayRectangle(j, -1, true);

                r1.X += 1;
                r1.Y += 1;
                r1.Width = r1.Width * 2 - 2;
                r1.Height = r1.Height / 2 - 2;

                using (SolidBrush br =
                    new SolidBrush(
                        this.dataGridView1.ColumnHeadersDefaultCellStyle.BackColor))
                {
                    e.Graphics.FillRectangle(br, r1);
                }

                using (Pen p = new Pen(SystemColors.InactiveBorder))
                {
                    e.Graphics.DrawLine(p, r1.X, r1.Bottom, r1.Right, r1.Bottom);
                }

                using (StringFormat format = new StringFormat())
                using (SolidBrush br =
                    new SolidBrush(
                        this.dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor))
                {
                    format.Alignment = StringAlignment.Center;
                    format.LineAlignment = StringAlignment.Center;

                    e.Graphics.DrawString(monthes[j / 2],
                        this.dataGridView1.ColumnHeadersDefaultCellStyle.Font,
                        br,
                        r1,
                        format);
                }

                j += 2;
            }
        }

        void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.RowIndex == -1 && e.ColumnIndex > -1)
            {
                e.PaintBackground(e.CellBounds, false);

                Rectangle r2 = e.CellBounds;
                r2.Y += e.CellBounds.Height / 2;
                r2.Height = e.CellBounds.Height / 2;
                e.PaintContent(r2);

                e.Handled = true;
            }
        }

 

标签:MultipleLayeredColumnHeader,r1,dataGridView,Add,dataGridView1,new,Height,Columns
From: https://www.cnblogs.com/robertyao/p/17445891.html

相关文章

  • 【WebView2】(二)WinForm 引入 WebView2 显示 Web 内容
    https://www.itsvse.com/thread-10362-1-1.html需求:使用VS2022新建一个.NETFramework4.7.2的WinForm项目,引用Microsoft.Web.WebView2SDK包,使用WebView2控件显示和渲染Web应用。MicrosoftEdgeWebView2控件允许在本机应用中嵌入web技术(HTML、CSS以及JavaSc......
  • winfrom中对datagridview控件更新数据并添加到数据库中
    第一种方法:双击鼠标直接修改数据同步到数据库1、首先在app.config配置数据库<?xmlversion="1.0"encoding="utf-8"?><configuration>   <connectionStrings>      <addname="local"         connectionString="DataSource=DESKTOP-H......
  • DataGridView完美解决复制粘贴功能
    //在DataGridView的PreviewKeyDown事件中privatevoiddataGridView1_PreviewKeyDown(objectsender,PreviewKeyDownEventArgse){if(e.Control&&e.KeyCode==Keys.V)//判断是否按下ctrl+v{Paste(dataGridView1,"",0,false);//粘贴代码}}粘贴,行数和列数不......
  • 为视障者打造无障碍的 WinForms 应用程序
    如何在WindowsForms应用程序中改善屏幕阅读器可访问性屏幕阅读器是一种辅助技术,可以通过语音或者盲文显示器来读出屏幕上的内容,帮助视力障碍者使用计算机。WindowsForms是一种基于.NETFramework的桌面应用程序开发技术,提供了丰富的控件和组件,以及一些可访问性功能,可以让开......
  • WinForm中给弹窗添加遮罩层
    1.新建三个窗体:  2.MainForm主窗体代码如下: privateMaskForm_shadowForm;///<summary>///显示遮罩层///</summary>privatevoidShowMask(){_shadowForm=newShadowForm......
  • Winform虚拟/模拟键盘
    项目需要在触摸屏上增加一个虚拟键盘。记录下过程中遇到的问题及解决方法。1.模拟按键网上找到如下3种方法1)SendKeys.Send测试单独的shift不好用,所以最终未采纳此方法SendKeys.Send("^{E}");//shift+eSendKeys.Send("{Enter}");2)keybd_event最终选择了这个方法,简单有效......
  • C# Winform按钮避免重复点击
    btn_01.Enabled=false;//执行任务的函数和代码//执行任务的函数和代码Application.DoEvents();btn_01.Enabled=true;就是让应用程序的消息队列自动走完(在按钮正常前清空消息队列即可)......
  • c# winform定时刷新
     Thread多线程publicpartialclassForm2:Form{//横向滚动条记录的是像素位数//竖向滚动条记录的行的索引值intVerticalScrollIndex=0;intHorizontalOffset=0;publicForm2(){Initializ......
  • 盘点界面控件DevExpress WinForms的几大应用程序主题
    DevExpressWinForm控件包含了50+个自定义皮肤,其中涵盖了MicrosoftOffice和Windows11启发式的应用程序主题。PS:DevExpressWinForm拥有180+组件和UI库,能为WindowsForms平台创建具有影响力的业务解决方案。DevExpressWinForm能完美构建流畅、美观且易于使用的应用程序,无论是Of......
  • Winform设置成默认以管理员方式启动的方法
     很多exe应用如果不以管理员权限运行,达不到运行目的,也会让用户很困扰。解决方法:1、在项目上右键添加新文件,选择新建app.manifest文件 2、按照下图参照注释部分修改trustInfo节点中的配置即可还有另外一个方法。这个需要写在Program.cs里面。staticvoidMain(s......