C# 如何在DataGridViewImageColumn上显示文本
在我们的应用程序中,我们使用datagridview控件来显示一些数据。 在gridview中,有一列是DataGridViewImageColumn 在CellFormatting事件中,我们设置了一些图像,如C# 如何在DataGridViewImageColumn上显示文本,c#,winforms,datagridview,datagridviewimagecolumn,C#,Winforms,Datagridview,Datagridviewimagecolumn,在我们的应用程序中,我们使用datagridview控件来显示一些数据。 在gridview中,有一列是DataGridViewImageColumn 在CellFormatting事件中,我们设置了一些图像,如 e.CellStyle.BackColor = Color.Red; e.Value = Properties.Resources.Triangle 其中三角形是位图资源,图像是透明的。当我们将颜色设置为红色时,图像的透明部分将显示颜色,并且工作正常 现在我们必须在图像上显示一些文本
e.CellStyle.BackColor = Color.Red;
e.Value = Properties.Resources.Triangle
其中三角形是位图资源,图像是透明的。当我们将颜色设置为红色时,图像的透明部分将显示颜色,并且工作正常
现在我们必须在图像上显示一些文本。
那么,有没有办法在DataGridViewImageColumn中显示的透明图像上显示文本呢?无需弄乱图像 相反,您可以自己控制单元格的绘制,可能如下所示:
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
矩形内
您可能需要查看..中丰富的数据集
如果文本取决于行,则可以使用
e.RowIndex
参数获取要为每个单元格显示的正确文本。在将文本指定给值之前,可以在图像上“绘制”文本。
@DonBoitnott:Ok。。所以你是说我需要得到图像,然后做一些事情,通过编程在图像上写一个文本?你解决了你的问题吗?