C# 从DataTable到DataGridView,包括图像路径

C# 从DataTable到DataGridView,包括图像路径,c#,sql,winforms,telerik,C#,Sql,Winforms,Telerik,我正在构建一个小应用程序。此应用程序存储客户信息和客户图片。 这里有一个问题,我想用图片将数据从datatable显示到datagridview。我的应用程序不会将图像存储在数据库中,而是存储在文件夹中 所以在我的数据库中只存储图像路径。但不是完整路径,只有文件夹或文件的名称。我在每个名称上使用合并路径。与保留完整路径相比,如果根目录已更改,则更容易管理文件夹 例如,如果我想在图片框中显示图像,我会这样做 //consider the record has been selected from

我正在构建一个小应用程序。此应用程序存储客户信息和客户图片。 这里有一个问题,我想用图片将数据从datatable显示到datagridview。我的应用程序不会将图像存储在数据库中,而是存储在文件夹中

所以在我的数据库中只存储图像路径。但不是完整路径,只有文件夹或文件的名称。我在每个名称上使用合并路径。与保留完整路径相比,如果根目录已更改,则更容易管理文件夹

例如,如果我想在图片框中显示图像,我会这样做

//consider the record has been selected from table

classDirectory cd = new classDirectory();

// it will check current directory combine application name folder and branch name folder.

string branchFolderPath = cd.getBranchFolderPath(branchName); 

string branchPanelPath = cd.getPanelFolderPath(branchFolderPath,panelName);

string customerPath = cd.getCustomerFolderPath(branchPanelPath,customerID + " - " + customerName);

string customerImage = path.Combine(customerPath,customerPicPath);

picCustomer.Image = new Bitmap(customerImage);
合并后,它将返回字符串“C:\Users\My Name\Documents\My Application Name\Branch Name\Panel Name\1235-HIDAYAH\Pictures\HIDAYAH-Picture.jpg”

这样我就可以看到照片了

但是,我不知道如何在网格视图中显示数据

到目前为止,我刚刚做到了

Customer.classDataBinding cdb = new Customer.classDataBinding();

DataTable dataCustomer = cdb._listOfCustomer();

dgvCustomer.DataSource = dataCustomer;

this.dgvCustomer.Columns["Customer Image"].Width = 120; //For a while, this code return the string only.
this.dgvCustomer.Columns["Customer ID"].Width = 120;
this.dgvCustomer.Columns["Name"].Width = 120;
this.dgvCustomer.Columns["Branch"].Width = 120;
this.dgvCustomer.Columns["Panel"].Width = 120;
在cdb中编码。\u客户列表()


有什么建议吗?希望有人能帮我解决这个问题。非常感谢

您正在使用Telerik网格吗?如果是这样,在绑定网格后,可以隐藏显示路径的列,添加GridViewImageColumn,并通过创建正确的路径来指定其值

尝试一下这种方法

更新1:以下是一个示例:

 public Form1()
    {
        InitializeComponent();

        Random r = new Random();
        DataTable table = new DataTable();
        table.Columns.Add("ID", typeof(int));
        table.Columns.Add("Name", typeof(string));
        table.Columns.Add("ImageName", typeof(string));

        table.Rows.Add(1, "Row 1", "copy.png");
        table.Rows.Add(2, "Row 2", "cut.png");
        table.Rows.Add(3, "Row 3", "folder.png");
        table.Rows.Add(4, "Row 4", "help.png");

        this.radGridView1.DataSource = table;

        radGridView1.Columns["ImageName"].IsVisible = false;

        GridViewImageColumn col = new GridViewImageColumn("Image");
        radGridView1.Columns.Add(col);

        foreach (GridViewRowInfo row in radGridView1.Rows)
        {
            string path = "..\\..\\"; //here you set your path
            row.Cells["Image"].Value = Image.FromFile( Path.Combine(path, row.Cells["ImageName"].Value.ToString()));

        }
    }

检查DataTable dataCustomer=cdb._listOfCustomer();。通过检查数据表的行集合,确保数据表中实际包含数据。让我们知道它是否有数据。谢谢评论。。。这意味着我需要使用每一个,对吗?不是数据源?谢谢你的帮助。。。我会尽力去做
 public Form1()
    {
        InitializeComponent();

        Random r = new Random();
        DataTable table = new DataTable();
        table.Columns.Add("ID", typeof(int));
        table.Columns.Add("Name", typeof(string));
        table.Columns.Add("ImageName", typeof(string));

        table.Rows.Add(1, "Row 1", "copy.png");
        table.Rows.Add(2, "Row 2", "cut.png");
        table.Rows.Add(3, "Row 3", "folder.png");
        table.Rows.Add(4, "Row 4", "help.png");

        this.radGridView1.DataSource = table;

        radGridView1.Columns["ImageName"].IsVisible = false;

        GridViewImageColumn col = new GridViewImageColumn("Image");
        radGridView1.Columns.Add(col);

        foreach (GridViewRowInfo row in radGridView1.Rows)
        {
            string path = "..\\..\\"; //here you set your path
            row.Cells["Image"].Value = Image.FromFile( Path.Combine(path, row.Cells["ImageName"].Value.ToString()));

        }
    }