datagridview 标题设置背景图片

        private void dgvCaozuoList_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.RowIndex == -1)//标题行
            {
                int X = e.CellBounds.X;
                int Y = e.CellBounds.Y;
                int W = e.CellBounds.Width;
                int H = e.CellBounds.Height;

                Pen blackPen = new Pen(Color.FromArgb(195, 195, 195));//边框颜色

                System.Drawing.Image image = Properties.Resources.tableheadbg;//背景图片
                TextureBrush tBrush = new TextureBrush(image);

                e.Graphics.FillRectangle(tBrush, new System.Drawing.Rectangle(X, Y, W, H));

                if (e.ColumnIndex == 0)
                    e.Graphics.DrawRectangle(blackPen, new System.Drawing.Rectangle(X, Y + 1, W - 1, H - 2));
                else
                    e.Graphics.DrawRectangle(blackPen, new System.Drawing.Rectangle(X - 1, Y + 1, W, H - 2));

                e.PaintContent(e.CellBounds);
                e.Handled = true;
            }
        }

 

给 dgvCaozuoList增加了一个CellPainting的事件,事件中,把标题行重新绘制了一下,增加了背景图片。其中背景图片是我们导入到程序中的。而不能使用非导入的图片

 

 

posted @ 2020-09-11 16:57  小了白了兔666  阅读(684)  评论(0)    收藏  举报