列にtype
自分で画像を描画する必要があります。もちろん、描画された画像はtext
(ファイルタイプの記述、例:、、.pdf
... .txt
)に対応しています。すべての画像を自分で準備する必要があります。不明なファイル タイプに対応する画像がない場合は、Unknown file type image
. セルに画像を描画するには、イベントを処理する必要がCellPainting
あります。試すことができるコードは次のとおりです。
//Dictionary to store the pairs of `text` and the corresponding image
Dictionary<string, Image> dict = new Dictionary<string, Image>(StringComparer.CurrentCultureIgnoreCase);
//load data for your dict
dict["Unknown"] = yourUnknownImage;//This should always be added
dict[".pdf"] = yourPdfImage;
dict[".txt"] = yourTxtImage;
//.....
//CellPainting event handler for your dataGridView1
//Suppose the column at index 1 is the type column.
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e){
if(e.ColumnIndex == 1 && e.RowIndex > -1){
var image = dict["Unknown"];
if(e.Value != null) {
Image img;
if(dict.TryGetValue(e.Value.ToString(), out img)) image = img;
}
//Draw the image
e.Graphics.DrawImage(image, new Rectangle(2,2, e.Bounds.Height-4, e.Bounds.Height-4));
}
}