如何:顯示 Windows Form DataGridView 控制項的儲存格影像
圖片或圖形是您可以在資料列中顯示的其中一個值。 這些圖形通常採用員工相片或公司標誌的形式。
當您在 DataGridView 控制項內顯示資料時,合併圖片很簡單。 DataGridView 控制項原生會處理 Image 類別支援的任何影像格式,以及某些資料庫使用的 OLE 圖片格式。
如果 DataGridView 控制項的資料來源具有影像的資料行,就會自動由 DataGridView 控制項顯示。
下列程式碼範例示範如何從內嵌資源擷取圖示,並將其轉換成點陣圖,以顯示在影像資料行的每個儲存格中。 如需以對應影像取代文字儲存格值的另一個範例,請參閱如何:自訂 Windows Forms DataGridView 控制項中的資料格式。
範例
private void createGraphicsColumn()
{
Icon treeIcon = new Icon(this.GetType(), "tree.ico");
DataGridViewImageColumn iconColumn = new DataGridViewImageColumn();
iconColumn.Image = treeIcon.ToBitmap();
iconColumn.Name = "Tree";
iconColumn.HeaderText = "Nice tree";
dataGridView1.Columns.Insert(2, iconColumn);
}
Public Sub CreateGraphicsColumn()
Dim treeIcon As New Icon(Me.GetType(), "tree.ico")
Dim iconColumn As New DataGridViewImageColumn()
With iconColumn
.Image = treeIcon.ToBitmap()
.Name = "Tree"
.HeaderText = "Nice tree"
End With
dataGridView1.Columns.Insert(2, iconColumn)
End Sub
編譯程式碼
這個範例需要:
名為
dataGridView1
的 DataGridView 控制項。名為
tree.ico
的內嵌圖示資源。System、System.Windows.Forms 和 System.Drawing 組件的參考。