c# DataGridView 的一些属性设置,序号,合并头

时间:2022-02-14 04:39:07
  this.dataGridView1.DataSource = this.dISASTERBindingSource;
            this.dataGridView1.Location = new System.Drawing.Point(0, 0);
            this.dataGridView1.Name = "dataGridView1" ;
            this.dataGridView1.RowTemplate.Height = 30;
            this.dataGridView1.Size = new System.Drawing.Size(773, 343);
            this.dataGridView1.TabIndex = 0;
            this.dataGridView1.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler (this.dataGridView1_CellContentClick);
            this.dataGridView1.CellEndEdit += new System.Windows.Forms.DataGridViewCellEventHandler (this.dataGridView1_endEdit);
            this.dataGridView1.AllowUserToResizeColumns = false;
            this.dataGridView1.AllowUserToResizeRows = false;
           
            /*
             * 设置最后边的背景色
             * */
            this.dataGridView1.BackgroundColor = System.Drawing.Color.FromArgb(240, 240, 240);
            /**
            *设置表头的高度 、对齐方式、颜色
            **/
            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode .DisableResizing;
            this.dataGridView1.ColumnHeadersHeight = 40;
          
            this.dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment .MiddleCenter;
            this.dataGridView1.RowHeadersDefaultCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment .MiddleCenter;
 
            this.dataGridView1.EnableHeadersVisualStyles = false;
            this.dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = System.Drawing.Color.YellowGreen;
            this.dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = System.Drawing.Color.BlueViolet;
           
            /**
            *设置第一列不可编辑
            **/
            this.dataGridView1.Columns[0].ReadOnly = true;
            /**
           *设置表格外边框样式
           **/
            this.dataGridView1.BorderStyle = System.Windows.Forms.BorderStyle.None;
            /**
           *设置表格内部的线条颜色
           **/
            this.dataGridView1.GridColor = System.Drawing.Color.WhiteSmoke;
            /**
           *去掉最后一行
           **/
            this.dataGridView1.AllowUserToAddRows = false;
            /**
           *左侧标题栏隐藏
           **/
            this.dataGridView1.RowHeadersVisible = false;
            /**
           *设置选中单元的背景色和字体颜色
           **/
            this.dataGridView1.DefaultCellStyle.SelectionBackColor = System.Drawing.Color.White;
            this.dataGridView1.DefaultCellStyle.SelectionForeColor = System.Drawing.Color.Black;
            /**
           *竖直方向填充满datagridview
           **/
            this.dataGridView1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode .Fill;
 
         
           /**
           *带链接的列
           **/
            private System.Windows.Forms. DataGridViewLinkColumn editColumn;
            this.editColumn.LinkColor = System.Drawing.Color.Blue;
            this.editColumn.UseColumnTextForLinkValue = true;
            this.editColumn.Text = "修改" ;
            this.editColumn.HeaderText = "编辑" ;
            this.editColumn.Name = "editColumn" ;
            this.editColumn.DefaultCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment .MiddleCenter;
 
   //datagridview前面加序号  直接在方法rowpostpaint中添加dataGridView_RowPostPaint这个方法就可以了  需要把rowheadervisible改为true
        private void dataGridView_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            SetDataGridViewRowXh(e, sender as DataGridView);
        }
        private void SetDataGridViewRowXh(DataGridViewRowPostPaintEventArgs e, DataGridView dataGridView)
        {
            SolidBrush solidBrush = new SolidBrush(dataGridView.RowHeadersDefaultCellStyle.ForeColor);
            int xh = e.RowIndex + 1;
            e.Graphics.DrawString(xh.ToString(CultureInfo.CurrentUICulture), e.InheritedRowStyle.Font, solidBrush, e.RowBounds.Location.X + 5, e.RowBounds.Location.Y + 4);
        }
         
 #region 重绘datagridview表头
            DataGridView dgv = (DataGridView )(sender);
            if (e.RowIndex == -1 && (e.ColumnIndex == 1 || e.ColumnIndex == 2))
            {
 
                //e.CellStyle.Font = new Font(dataGridView1.DefaultCellStyle.Font, FontStyle.Bold);
                //e.CellStyle.WrapMode = DataGridViewTriState.True;
                if (e.ColumnIndex == 1)
                {
                    top = e.CellBounds.Top;
                    left = e.CellBounds.Left;
                    height = e.CellBounds.Height;
                    width1 = e.CellBounds.Width;
                }
 
 
                int width2 = this .dataGridView1.Columns[2].Width;
 
                Rectangle rect = new Rectangle(left, top, width1 + width2, e.CellBounds.Height);
                using (Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor))
                {
                    //抹去原来的cell背景
                    e.Graphics.FillRectangle(backColorBrush, rect);
                }
 
                using (Pen gridLinePen = new Pen(dgv.GridColor))
                {
                    //画表头的左、上、右、下边线
                    //e.Graphics.DrawLine(gridLinePen, left, top , left, top + height);
                    e.Graphics.DrawLine(gridLinePen, left, top , left + width1 + width2-1, top );
                    e.Graphics.DrawLine(gridLinePen, left + width1 + width2 - 1, top, left + width1 + width2 - 1, top + height-2);
                    e.Graphics.DrawLine( new Pen (Color.FromArgb(160, 160, 160)), left, top + height - 1, left + width1 + width2-1, top + height - 1);
                    //计算绘制字符串的位置
                    string columnValue = "编辑" ;
                    SizeF sf = e.Graphics.MeasureString(columnValue, e.CellStyle.Font);
                    float lstr = (width1 + width2 - sf.Width) / 2;
                    float rstr = (height - sf.Height) / 2;
 
                    //画出文本框
                    if (columnValue != "" )
                    {
                        e.Graphics.DrawString(columnValue, e.CellStyle.Font,
                                                   new SolidBrush (e.CellStyle.ForeColor),
                                                     left + lstr,
                                                     top + rstr,
                                                     StringFormat.GenericDefault);
                    }
 
                }
                e.Handled = true;
 
 
            }
            #endregion

c# DataGridView 的一些属性设置,序号,合并头的更多相关文章

  1. c# WinForm开发 DataGridView控件的各种操作总结(单元格操作,属性设置)

    一.单元格内容的操作 *****// 取得当前单元格内容 Console.WriteLine(DataGridView1.CurrentCell.Value); // 取得当前单元格的列 Index ...

  2. 转:c# WinForm开发 DataGridView控件的各种操作总结(单元格操作,属性设置)

    一.单元格内容的操作 *****// 取得当前单元格内容 Console.WriteLine(DataGridView1.CurrentCell.Value); // 取得当前单元格的列 Index  ...

  3. DEVexpress GridControl 属性设置

    1. 如何解决单击记录整行选中的问题 View->OptionsBehavior->EditorShowMode 设置为:Click 2. 如何新增一条记录 (1).gridView.Ad ...

  4. DEV控件GridControl常用属性设置

    1. 如何解决单击记录整行选中的问题 View->OptionsBehavior->EditorShowMode 设置为:Click 2. 如何新增一条记录 (1).gridView.Ad ...

  5. DevExpress GridView属性设置 z

    本文主要总结控件的属性设置,附上图片,给大家一个参考.后续会给大家分享功能实现和使用的小技巧. GirdControl是数据的容器,它包含多种显示方式,GridView则是一种二维表格视图. 绑定数据 ...

  6. DataGridView 列自适应宽度 设置

    代码: Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; 或者设置DataGridView的AutoSizeColu ...

  7. white-space 属性设置如何处理元素内的空白

    定义和用法white-space 属性设置如何处理元素内的空白. 这个属性声明建立布局过程中如何处理元素中的空白符.值 pre-wrap 和 pre-line 是 CSS 2.1 中新增的. 默认值: ...

  8. (转)DevExpress GridView属性设置

    GirdControl是数据的容器,它包含多种显示方式,GridView则是一种二维表格视图. 绑定数据源: List<Student> list = new List<Studen ...

  9. DEV控件GridControl常用属性设置(转)

      1. 如何解决单击记录整行选中的问题 View->OptionsBehavior->EditorShowMode 设置为:Click 2. 如何新增一条记录 (1).gridView. ...

随机推荐

  1. &lbrack;Asp&period;net 5&rsqb; Localization-简单易用的本地化-全球化信息

    本篇比较简单介绍Localization解决方案中: Microsoft.Framework.Globalization.CultureInfoCache 工程 CultureInfoGenerato ...

  2. PHPExcel使用体会

    PHPExcel使用体会 因为毕设导师智能分配系统的需要,系负责人在管理学生和导师时,希望可以使用Excel批量导入学生和导师的信息,学长的报课系统使用的是PHPExcel的类库,于是我也抽空花了2天 ...

  3. Download InstallShield Limited Edition for Visual Studio 地址

    http://learn.flexerasoftware.com/content/IS-EVAL-InstallShield-Limited-Edition-Visual-Studio?lang=10 ...

  4. undefined local variable or method &OpenCurlyQuote;xxx’ for &num;&lt&semi;RSpec&colon;&colon;Core&colon;&colon;ExampleGroup&colon;&colon;Nested&lowbar;1&colon;&colon;Nested&lowbar;1&colon;0xbc88d6c&gt&semi;错误解决方案

    官方文档如下: https://github.com/rspec/rspec-rails/blob/master/Capybara.md Capybara 2.0 To use Capybara 2. ...

  5. apache开源项目--HydraBase

    Facebook 在官方博客上宣布推出HBase数据库的升级版——HydraBase, Facebook是HBase的重度用户,Facebook的HBase数据库系统存储着Facebook的很多关键业 ...

  6. HW5&period;13

    public class Solution { public static void main(String[] args) { System.out.printf("%s\t%s\n&qu ...

  7. yum软件管理器,及yum源配置

    说到yum源就必须说到linux系统中特有的依赖关系问题,yum就是为了解决依赖关系而存在的.yum源就相当是一个目录项,当我们使用yum机制安装软件时,若需要安装依赖软件,则yum机制就会根据在yu ...

  8. 530&period; Minimum Absolute Difference in BST

    Given a binary search tree with non-negative values, find the minimum absolute difference between va ...

  9. Docker学习——Lepus部署

    Lepus部署(基于docker)及mysql慢查询配置 介绍 Lepus是一个由Python+PHP开发的数据库企业级监控系统,可用于MySQL/Oracle/MongoDB/Redis 下载镜像 ...

  10. Visual Studio 2010 Shortcuts

    Uploaded by ProNotion, updated on 11/28/2013 by jmb Platform: Windows/ English  PDF    Print   Hide ...