0

DataGridViewバインディング アダプタの 1 つにバインドしています。私のグリッドに"type"は、添付ファイル (つまり「.pdf」) に対応する列があります。これは、グリッド ビューの列にテキストとして表示されています (予想どおり)。タイプを示すために、列の値を画像に変更できるようにしたいと考えています。たとえば、タイプが a の場合、 text ではなく列PDFにドキュメントの画像が必要です。PDF".pdf"

セルが追加されたときにこれを動的に行う方法はありますか? それとも、すべてのセルがロードされた後に何かを行う必要がありますか?

乾杯。

4

2 に答える 2

0

列に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));
   }
}
于 2013-09-06T13:37:56.893 に答える