您好我有一个问题,即使用bindingSource将数据传输到datagridview,我的代码如下;
string query= "SELECT a.id as id1,a.id2, a.system_id, CONCAT(b.path,a.image) as photo ,b.id as id5,b.name,b.path FROM users a INNER JOIN systems b ON a.system_id=b.id order by a.id DESC LIMIT 20";
using (MySqlCommand cmd = new MySqlCommand(query, conn))
{
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
var dt = new System.Data.DataTable();
da.Fill(dt);
bindingSource1.DataSource = dt.Rows.Cast<DataRow>().Select(p => new aclass() { id = (p["id1"].ToString()), Photo= Image.FromFile(p["photo enter code here"].ToString()) }).ToList();
conn.Close();
}
此功能获取图像路径并添加到列表并在datagridview中显示照片,但我的问题是如果这张照片被删除没有显示错误,如果这张照片被删除如何删除添加一张照片头像默认。如果存在照片显示原始照片。 Thnks
答案 0 :(得分:0)
You need to check if the file exists first and then either load it from the path or from the avatar path. You can use these helper functions:
private string LoadImage(string imagePath) {
return Image.FromFile(MapImagePath(imagePath));
}
private string MapImagePath(string imagePath) {
if (string.IsNullOrEmpty(imagePath))
imagePath = GetNoImagePath();
else {
if (!File.Exists(imagePath))
imagePath = GetNoImagePath();
}
return imagePath;
}
private string GetNoImagePath() {
return "D:\ProjectFolder\Images\NoImage.png";
}
And now you can call them from your code like this:
var data = dt.Rows.Cast<DataRow>().Select(p => new aclass() {
id = (p["id1"].ToString()),
Photo = LoadImage(p["photo enter code here"].ToString())
}).ToList();
Note: Of course, you can make the path to the avatar a constant instead of a function. The function could be useful if you have several avatars and you want to return one of them based on some conditions.
答案 1 :(得分:0)
You need to check if the file exists first and then either load it from the path or from the avatar path.
string avaterPath=@"D:\Images\NoImage.png";
string query= "SELECT a.id as id1,a.id2, a.system_id, CONCAT(b.path,a.image) as photo ,b.id as id5,b.name,b.path FROM users a INNER JOIN systems b ON a.system_id=b.id order by a.id DESC LIMIT 20";
using (MySqlCommand cmd = new MySqlCommand(query, conn))
{
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
var dt = new System.Data.DataTable();
da.Fill(dt);
bindingSource1.DataSource = dt.Rows.Cast<DataRow>().Select(p => new aclass() { id = (p["id1"].ToString()), Photo=File.Exists(p["photo enter code here"].ToString())? Image.FromFile(p["photo enter code here"].ToString()):Image.FromFile(avaterPath) }).ToList();
conn.Close();
}
答案 2 :(得分:0)
感谢大家,这是我的解决方案;
string avaterPath=@"D:\Images\NoImage.png";
string query= "SELECT a.id as id1,a.id2, a.system_id, CONCAT(b.path,a.image) as photo ,b.id as id5,b.name,b.path FROM users a INNER JOIN systems b ON a.system_id=b.id order by a.id DESC LIMIT 20";
using (MySqlCommand cmd = new MySqlCommand(query, conn))
{
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
var dt = new System.Data.DataTable();
da.Fill(dt);
bindingSource1.DataSource = dt.Rows.Cast<DataRow>().Select(p => new aclass() { id = (p["id1"].ToString()), Photo=File.Exists(p["photo enter code here"].ToString())? Image.FromFile(p["photo enter code here"].ToString()):Image.FromFile(avaterPath) }).ToList();
conn.Close();
}