简体   繁体   English

具有图像路径的C#datagridview绑定源(如果不存在)

[英]C# datagridview binding source with image path if not existing

Hello i have one question, im getting data to datagridview with bindingSource, my code is bellow; 您好我有一个问题,即时通讯使用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();


            }

This function getting image path and adding to list and showing in datagridview with photo, but my problem is if this photo is deleted not getting showing error,how to make if this photo is deleted adding one photo avatar default. 该功能获取图像路径并添加到列表中并与照片一起显示在datagridview中,但是我的问题是如果删除此照片未显示错误,如何删除该照片并添加默认的一张照片头像。 If exist photo showing original photo. 如果有,则显示原始照片的照片。 Thnks Thnks

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. 如果您有多个头像,并且要根据某些条件返回其中一个,则该功能可能会很有用。

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();


            }

thanks everyone, this is my solution; 谢谢大家,这是我的解决方案;

 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();


            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM