在我们的应用程序中,我们使用datagridview控件来显示一些数据。在网格视图中,有一列是DataGridViewImageColumn。
在CellFormatting事件中,我们设置了一些图像,如
e.CellStyle.BackColor = Color.Red;
e.Value = Properties.Resources.Triangle其中Triangle是位图资源,图像是透明的。当我们将颜色设置为红色时,图像的透明部分将显示该颜色,并且工作正常。
现在我们必须在图像上显示一些文本。那么,有没有办法在DataGridViewImageColumn中显示的透明图像上显示文本呢?
发布于 2014-10-02 06:21:05
不需要弄乱图像。
相反,您可以自己控制单元格的绘制,可能如下所示:
private void dataGridView1_CellPainting(object sender,
DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == yourImageColumnIndex)
{
e.PaintBackground(e.ClipBounds, true);
e.PaintContent(e.ClipBounds);
e.Graphics.DrawString(yourText, dataGridView1.Font, Brushes.Yellow,
e.CellBounds.X, e.CellBounds.Y);
e.Handled = true;
}
}正如您所看到的,大部分工作都是由系统完成的;您只需添加一行即可绘制文本。当然,您肯定会希望使用不同的字体,并且可能还会更改所有其他参数。请留在e.CellBounds矩形内。
您可能想要查看一下event's aguments中的丰富数据集。
如果您的文本依赖于行,您可以使用e.RowIndex参数来获取要为每个单元格显示的正确文本。
https://stackoverflow.com/questions/26120221
复制相似问题