1

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

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

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

//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._listOfCustomer()中的代码

  public DataTable _listOfCustomer()
    {
        cs.testConnection();

        DataTable dtCustomer = new DataTable();

        using (SqlConnection conn = new SqlConnection(cs.connString))
        {
            string query = "SELECT tbl_customer.customerPicPath as [Customer Image], tbl_customer.customerID as [Customer ID],tbl_customer.customerName as Name, tbl_branch.branchName as Branch, tbl_panel.panelName as Panel FROM (tbl_branch INNER JOIN tbl_panel ON tbl_branch.branchID = tbl_panel.branchID) INNER JOIN tbl_customer ON tbl_panel.panelID = tbl_customer.panelID;";

            using (SqlDataAdapter adap = new SqlDataAdapter(query, conn))
            {
                adap.Fill(dtCustomer);

            }
        }

        return dtCustomer;

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

4

1 回答 1

2

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

试试这种方法。

UPDATE1:这是一个例子:

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

        }
    }
于 2012-11-07T12:11:07.393 回答