想做一个如下图所示的效果:然后在网上找到了实现的代码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 基础结构,但不适合在代码中直接使用”又没说该怎么用啊求大侠解惑,谢谢
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
诶,这个思路倒是不错,值得一试,谢谢。但是重写PaintBackground这个方法没行通还是让我感觉遗憾。