当前位置:编程学习 > C#/ASP.NET >>

如何调用datagridview的backgroundimage属性?

想做一个如下图所示的效果:

然后在网上找到了实现的代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
  
namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.dataGridView1.BackgroundImage = Image.FromFile("c:\\2216400.jpg");
            this.dataGridView1.DefaultCellStyle.BackColor = Color.FromArgb(128, Color.White);
            this.dataGridView1.DefaultCellStyle.SelectionBackColor = Color.FromArgb(128, Color.Blue);
        }
    }
  
    public class MyDataGrid : DataGridView
    {
        protected override void PaintBackground(Graphics graphics, Rectangle clipBounds, Rectangle gridBounds)
        {
            graphics.DrawImageUnscaledAndClipped(this.BackgroundImage, gridBounds);
        }
  
        protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
        {
            if (e.RowIndex == -1 || e.ColumnIndex == -1)
            {
                return;
            }
            Rectangle newRect = new Rectangle(e.CellBounds.X + 1,
                e.CellBounds.Y + 1, e.CellBounds.Width - 4,
                e.CellBounds.Height - 4);
  
            using (
                Brush gridBrush = new SolidBrush(this.GridColor),
                backColorBrush = new SolidBrush(e.CellStyle.BackColor),
                selectedColorBrush = new SolidBrush(e.CellStyle.SelectionBackColor))
            {
                using (Pen gridLinePen = new Pen(gridBrush))
                {
                    if (this.Rows[e.RowIndex].Selected)
                    {
                        e.Graphics.FillRectangle(selectedColorBrush, e.CellBounds);
                    }
                    else
                    {
                        e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
                    }
                    if (e.Value != null)
                    {
                        e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
                            Brushes.Black, e.CellBounds.X + 2,
                            e.CellBounds.Y + 2, StringFormat.GenericDefault);
                    }
                }
                e.Handled = true;
            }
        }
    }
}


但是最为关键的datagridview的backgroundimage属性根本就无法调用啊,这位大牛所写的this.BackgroundImage在智能输入里面根本就没有啊。

强行这样写的话,就报错了。

MSDN的关于DataGridView.BackgroundImage的注解在这里http://msdn.microsoft.com/zh-cn/library/system.windows.forms.datagridview.backgroundimage%28v=VS.80%29.aspx

但是光说“此属性支持 .NET 基础结构,但不适合在代码中直接使用”又没说该怎么用啊

求大侠解惑,谢谢 --------------------编程问答-------------------- 你是要透明吗?
DataGridView1.DefaultCellStyle.BackColor = Color.Transparent --------------------编程问答-------------------- 不是,我是需要在datagridview里面添加背景图片 --------------------编程问答--------------------
引用 1 楼 chinajiyong 的回复:
你是要透明吗?
DataGridView1.DefaultCellStyle.BackColor = Color.Transparent

错了,你是要设置dgv的背景图片,其实你可以换个思路来想,比如把dgv放到一个pannel里面,设置pannel的背景图片,设置dgv透明。 --------------------编程问答--------------------
引用 3 楼 chinajiyong 的回复:
引用 1 楼 chinajiyong 的回复:

你是要透明吗?
DataGridView1.DefaultCellStyle.BackColor = Color.Transparent

错了,你是要设置dgv的背景图片,其实你可以换个思路来想,比如把dgv放到一个pannel里面,设置pannel的背景图片,设置dgv透明。


诶,这个思路倒是不错,值得一试,谢谢。

但是重写PaintBackground这个方法没行通还是让我感觉遗憾。 --------------------编程问答--------------------
引用 4 楼 shisuoking 的回复:
引用 3 楼 chinajiyong 的回复:

引用 1 楼 chinajiyong 的回复:

你是要透明吗?
DataGridView1.DefaultCellStyle.BackColor = Color.Transparent

错了,你是要设置dgv的背景图片,其实你可以换个思路来想,比如把dgv放到一个pannel里面,设置pannel的背景图片,设置dgv透明。
……


试了一下,结果失败了。

透明倒是透明了,背景图片也能看到了,但是如果数据一多,出现下拉条的话,一拉动就杯具了,整个页面都花了 --------------------编程问答-------------------- 自己再顶一下
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,