我正在构建一个小型应用程序。此应用程序存储客户信息和客户图片。在这里,我有一个问题,我想用图片将数据从数据表显示到数据网格视图。我的应用程序不将图像存储在数据库中,而是存储在文件夹中。
所以在我的数据库中只存储图像路径。但不是完整路径,只有文件夹或文件的名称。我在每个名称上使用组合路径。与保留完整路径相比,如果根目录已更改,则管理文件夹会更容易。
例如,如果我想在图片框中显示图像,我会这样做。
//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;
有什么建议吗?希望有人可以帮助我解决这个问题。非常感谢!