2014-09-30 32 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在右邊的文本去顯示每個細胞。