首先,要添加圖片列,綁定數據的時候會觸發CellFormatting事件,在事件中取出圖片路徑,讀取圖片賦值給當前單元格。
private void dataGridview1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e){ if (dataGridview1.Columns[e.ColumnIndex].Name.Equals("Image")) { string path = e.Value.ToString(); e.Value = GetImage(path); }}public System.Drawing.Image GetImage(string path){ System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open); System.Drawing.Image result = System.Drawing.Image.FromStream(fs); fs.Close(); return result;}
以上就是DataGridView如何顯示圖片一些代碼片段,希望能給大家一個參考,也希望大家多多支持武林網。
新聞熱點
疑難解答