2012-11-01 3 views
1

작은 응용 프로그램을 만들고 있습니다. 이 응용 프로그램은 고객 정보 및 고객 사진을 저장합니다. 여기에 나는 datatable에서 datagridview로 데이터를 보여주고 싶을 때 문제가있다. 내 응용 프로그램은 이미지를 데이터베이스에 저장하지 않고 폴더에 저장합니다.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 : \ 사용자는 내 이름 \ 문서 내 응용 프로그램 이름 \ 지점 이름 \ 패널 이름 \ 1235 \ \ - HIDAYAH \ 사진 \의 HIDAYAH - picture.jpg 대신"

그래서 사진을 볼 수 있습니다.

그러나 그리드보기에서 데이터를 표시하는 방법을 모르겠습니다.

그래서 지금까지 방금 만들었습니다. 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; 

어떤 제안에

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; 

코드? 누군가가 나를이 문제를 해결하는 데 도움이되기를 바랍니다. 고마워요!

+0

체크 DataTable을 dataCustomer cdb._listOfCustomer =(); 행 집합을 검사하여 데이터가 실제로 데이터를 가지고 있는지 확인하십시오. 데이터가 있는지 알려주십시오. – KreepN

답변

2

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

     } 
    } 
+0

의견을 주셔서 감사합니다 ... 그 뜻 foreach를 사용해야합니까? 데이터 소스가 아닌가요? – Chuki2

+0

예, foreach를 사용하여 예제를 사용하여 답변을 업데이트했습니다 – checho

+0

도움을 주셔서 감사합니다 ... 할 생각입니다. – Chuki2