C#:DataGridView边框线、标题、单元格的各种颜色

1,常用设置:

//包含Header所有的单元格的背景色
            dataGridView1.DefaultCellStyle.BackColor = Color.Black;
            //包含Header所有单元格的前景色
            dataGridView1.DefaultCellStyle.ForeColor = Color.White;
            //Header以外所有有的单元格背景色
            dataGridView1.RowsDefaultCellStyle.BackColor = Color.WhiteSmoke;
            //奇数行的单元格的背景色
            dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.SkyBlue;
            //列Header的背景色
            dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.SkyBlue;
            //行Header的背景色
            dataGridView1.RowHeadersDefaultCellStyle.BackColor = Color.SkyBlue;
            //边框线的颜色
            dataGridView1.GridColor = Color.Black;
            //边框线的颜色生效需要另外设置
            dataGridView1.ColumnHeadersBorderStyle =DataGridViewHeaderBorderStyle.Single;
            //如果想让标题的边框线和颜色生效需要如下设置
            dataGridView1.EnableHeadersVisualStyles = false;

            //如果想自定义列头宽度
            dataGridView1.ColumnHeadersHeight = 20;
            //则需要进行如下设置:
            dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;

2,冻结列设置:

列属性:Frozen=True时冻结该列,False时不冻结,默认为False。

3,设置列标题居中。

4,使用DataTable作为数据源进行填充时可实现简单刷选:

       1, 通过DataTable的属性:DefaultView,获取DataView。

        2,通过Dataview中属性RowFilter,设置过滤条件

        3,将Dataview作为数据源赋值给DataGridView控件。

public virtual string RowFilter { get; set; },所需要的值大致等同于Sql筛选条件的字符串。

示例:

SQL语句:select * from tb_Info where id=1 and Description like '%学%'

获取DataTable,DataTable赋值给DataGridView

在DataGridView中呈现的效果等同于

SQL语句: select * from tb_Info;

获取DataTable,通过DataTable获取DataView

设置DataView.RowFilter =“id=1 and Description like '%学%'”;

5,利用行的绘制事件RowPostPaint给DataGridView添加行号。

private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            string index = (e.RowIndex + 1).ToString();
            Font font = e.InheritedRowStyle.Font;
            Color foreColor = e.InheritedRowStyle.ForeColor;
            Graphics g = e.Graphics;
            SizeF size = g.MeasureString(index, font);
            PointF p = new PointF(e.RowBounds.Left + 15, e.RowBounds.Top + (e.RowBounds.Height-size.Height)/2);
            g.DrawString(index, font, new SolidBrush(foreColor), p);

        }

6,数据绑定。

    使用List<T>作为DataGridView数据源时,List<T>.Count必须大于0,即不能是空集合。如果是空集合,在点击DataGridView时将抛出“索引溢出”异常。
    使用BindingList<T>作为数据源来可避免以上情况。

7,DataGridView添加序列号。

 private void dgview01_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            //给表格绘制序列号
            int index = e.RowIndex + 1;
            Rectangle rec = e.RowBounds;
            Graphics g = e.Graphics;
            Size s = dgview01.Rows[e.RowIndex].HeaderCell.Size;
            SizeF size = g.MeasureString(index.ToString(), new Font("宋体", dgview01.DefaultCellStyle.Font.Size));
            PointF p = new PointF(rec.Left + (s.Width - size.Width) / 2, rec.Top + (s.Height - size.Height) / 2);
            g.DrawString(index.ToString(), new Font("宋体", 10), Brushes.Black, p);
        }

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值