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

2 篇文章 0 订阅

本文为转载文章,原文章链接:

https://www.cnblogs.com/yanjinliang/p/5646829.html

  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

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值