我想从图像使用.net组件DataGridView给行标题和列标题一个自定义的背景。甚至可以这样做吗?如果是这样,怎么样?给一个dataGridView单元格在C#中的背景图像
我使用Visual Studio 2008,Windows应用程序,C#。
我想从图像使用.net组件DataGridView给行标题和列标题一个自定义的背景。甚至可以这样做吗?如果是这样,怎么样?给一个dataGridView单元格在C#中的背景图像
我使用Visual Studio 2008,Windows应用程序,C#。
它可能改变datagridview rowheader的属性。您需要处理CellPainting或RowPostPaint事件,并在行标题单元格中手动绘制图像。
protected override void OnRowPostPaint(DataGridViewRowPostPaintEventArgs e)
{
// Your code to insert image/content per cell goes here
}
在这样做,这是把每个标题元素的名称的CssClass在RowDataBound事件是这样,并在指定的CSS背景图片。
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
foreach (TableCell c in e.Row.Cells)
{
c.CssClass = "bgimage";
}
}
}
CSS:
.bgimage{ background-image:url(images/arrow-red-large-down.gif);
以及我如何能够定义css本身。它应该是一个外部文件? – Pieter888 2009-11-13 09:11:06
正确的方法是包含对外部样式表的引用,但可以在页面上内联样式。 – 2010-01-08 17:49:57
这是ASP.net,而不是winforms – 2010-04-08 16:04:03
我仍然无法弄清楚这些代码如何能帮助我实现这一目标。 我确实试图重写RowPostPaint事件,但我仍然不知道如何使用它来为单元格的背景插入图像 – Pieter888 2009-11-12 13:21:17
没关系我想通了,谢谢QuBaR! – Pieter888 2009-11-12 14:53:57
未来的谷歌浏览器:同样的技巧可以用于单元格为您的单元格提供渐变背景(使用'LinearGradientBrush'),仅使用OnRowPrePaint,并将单元格的默认背景颜色设置为* Transparent * – 2010-04-08 16:17:57