2016-08-10 4 views
-1

productID, productName 및 productPrice 열과 함께 datatables dt1이 있고 orderID, productID, productName, productPrice 및 totalPrice가있는 dt2가 있습니다. productID를 입력하는 동안 dt2에 dt1 정보를 얻으려면 어떻게해야합니까?한 데이터 테이블의 특정 열을 다른 데이터 테이블에 어떻게 추가합니까?

+0

자세한 정보 (일부 코드 등)를 입력 해주세요. 뭐하고 싶어? 이 데이터를 표시 하시겠습니까? – Arkowsky

+0

텍스트 상자에 ProductID 만 입력하면 dt1의 3 열 값을 dt2에 저장하려고합니다. 그 결과를 datagridview에 표시합니다. –

답변

0

당신을 올바르게 이해할 수 있을지 모르겠다. 어떻게 읽는가? C# datatable에서 JOIN 기능을 원한다. 이게 오래 전에 그렇게 생각한 더 나은 방법이 있습니다 ...

/// <summary> 
    /// Give a Table that contains the foreignkeys it returns a Table with a Description instead of the Key 
    /// The ID in the source has to be Column[0] 
    /// </summary> 
    /// <param name="foreignkeys">Foreignkey in you Table</param> 
    /// <param name="source">The Table the foreignkey refers to</param> 
    /// <param name="_foreigncolumn">Header of Foreignkeycolumn</param> 
    /// <param name="_sourcecolumn">Header description column in source Table</param> 
    /// <returns></returns> 
    public DataTable getDatabyForeignKey(DataTable foreignkeys, DataTable source, string _foreigncolumn, string _sourcecolumn) 
    { 
     try 
     { 
      DataTable displayname = new DataTable(); 
      displayname.Columns.Add("ID"); 
      //Maybe change that to a parameter as well (amount + header) 
      displayname.Columns.Add("Description"); 

      for (int i = 0; i < foreignkeys.Rows.Count; i++) 
      { 
       for (int j = 0; j < source.Rows.Count; j++) 
       { 
        string s = source.Rows[j][0].ToString(); 
        string ss = foreignkeys.Rows[i][_foreigncolumn].ToString(); 
        if (source.Rows[j][0].ToString() == foreignkeys.Rows[i][_foreigncolumn].ToString()) 
        { 
         foreignkeys.Rows[i][_foreigncolumn] = source.Rows[j][_sourcecolumn]; 
        } 
       } 

      } 
     } 
     catch (Exception) 
     { 
      MessageBox.Show(ex.Message);    
     } 

     return foreignkeys; 
    } 
관련 문제