2014-09-30 85 views
0

在我们的应用程序中,我们使用datagridview控件来显示一些数据。 在gridview中,一列是DataGridViewImageColumn。如何在DataGridViewImageColumn上显示文本

在CellFormatting情况下,我们设置一些相似图片

e.CellStyle.BackColor = Color.Red; 
e.Value = Properties.Resources.Triangle 

凡三角是一个位图资源和图像是透明的。当我们将颜色设置为红色时,图像的透明部分将显示颜色并且工作正常。

现在我们必须在图像上显示一些文字。 那么有没有什么方法可以在DataGridViewImageColumn中显示的透明图像上显示文本?

+0

您可以在将图像上的文字分配给“值”之前“绘制”文本。 – DonBoitnott 2014-09-30 13:32:46

+0

@DonBoitnott:好的..所以你说我需要得到图像,并做一些事情,以编程方式在图像上写文本? – 2014-10-01 02:34:04

+0

你解决了你的问题吗? – TaW 2014-10-07 15:31:02

回答

2

不需要搞乱图像。

相反,你可以自己控制小区的画,也许是这样的:

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矩形内。

你可能想HABE看看在event's aguments丰富的数据集..

如果您的文本依赖于该行,你可以使用e.RowIndex PARAM在右边的文本去显示每个细胞。