public class SetImg
{
public List<string> GetImgsrc()
{
List<string> listImgsrc = new List<string>();
string sql = "select imge from ws"; //查询数据中imge的数据(添加数据格式****.jpg)
DataTable dt = DBHelper.Select(sql);//执行查询语句,返回一个表。
foreach (DataRow row in dt.Rows)
{
listImgsrc.Add(row["imge"].ToString());//将数据一条一条的加入的list中
}
return listImgsrc;//返回这个集合
}
}
SetImg st = new SetImg();
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;
}
dgv中手动添加如下
选中dgv事件dataGridView2_CellFormatting_1
private void dataGridView2_CellFormatting_1(object sender, DataGridViewCellFormattingEventArgs e)
{
if (dataGridView2.Columns[e.ColumnIndex].Name.Equals("imge"))
{
var list = st.GetImgsrc();
string path = db.INIhelp.GetValue("DZ", "dz") + "\\" + list[e.RowIndex];
dataGridView2.Rows[e.RowIndex].Cells["img"].Value = GetImage(path);//依次为DataGridView赋值
}
}
运行结果
注意 如一直报index长度什么 就在窗体加载事件中:
dataGridView2.AllowUserToAddRows = false;//去除最后一行空白
如果对您有帮助的话点个小心心谢谢