2014-02-14 80 views
1

我有一個數組「MyArray」,並希望此數組添加到位置spcified的數據表中。第二行顯示我要將itemarray指定爲位置的位置3行向前。我爲單個字符串做了同樣的過程,比如datarow.insertAt [i]它的工作正常,但是當我在特定位置插入一堆數組時,它不起作用。如何在DataTable中的特定位置添加ItemArray到DataRow

string[] MyArray= new string[3]; 
     MyArray[0] = "Tom"; 
     MyArray[1] = "Canada"; 
     MyArray[2] = "+42-54948354-9";    
DataTable table = new DataTable(); 
       table.Columns.Add("Name"); 
       table.Columns.Add("Address"); 
       table.Columns.Add("CellNo");       
       DataRow drow; 
       drow = table.NewRow(); 
       drow.ItemArray[2] = MyArray; //Here I want to Add Array to DataRow at position 3 rows forward. 
       table.Rows.Add(drow);   

回答

2
string[] MyArray= new string[3]; 
     MyArray[0] = "Tom"; 
     MyArray[1] = "Canada"; 
     MyArray[2] = "+42-54948354-9";    
DataTable table = new DataTable(); 
       table.Columns.Add("Name"); 
       table.Columns.Add("Address"); 
       table.Columns.Add("CellNo");       
       DataRow drow; 
       drow = table.NewRow(); 
       drow.ItemArray = MyArray; 
       table.Rows.Add(drow); 

要添加特定的索引行,你可以使用InsertAt但如果在表中沒有行,那麼你不能在位置2(索引1)插入因爲沒有排在第1位例如下面的工作:

string[] MyArray = new string[3]; 
     MyArray[0] = "Tom"; 
     MyArray[1] = "Canada"; 
     MyArray[2] = "+42-54948354-9"; 
     DataTable table = new DataTable(); 
     table.Columns.Add("Name"); 
     table.Columns.Add("Address"); 
     table.Columns.Add("CellNo"); 
     for (int j = 0; j < 2; j++) 
     { 
      DataRow drow; 
      drow = table.NewRow(); 
      drow.ItemArray = MyArray; 
      table.Rows.InsertAt(drow, j); 
     } 

編輯2:

string[] MyArray = new string[3]; 
     MyArray[0] = "Tom"; 
     MyArray[1] = "Canada"; 
     MyArray[2] = "+42-54948354-9"; 
     DataTable table = new DataTable(); 
     //table.Columns.Add("Name"); 
     //table.Columns.Add("Address"); 
     //table.Columns.Add("CellNo"); 
     DataRow drow; 
     drow = table.NewRow(); 
     for (int j = 0; j < 2; j++) 
     { 
      table.Columns.Add(j.ToString()); 
      string s = string.Join(" ", MyArray); 
      drow[j.ToString()] = s; 
     } 
     table.Rows.Add(drow); 
+0

是的,你是對的我已經測試過,但如果我想在特定的位置添加數組,所以我會做什麼。像drow.ItemArray [i]。我是一個基於索引的位置 –

+0

請參閱我的編輯。它的表格不是Row的位置。如果表格中沒有任何指定的索引,那麼它將被添加到位置1。 – Kashif

+0

它的工作,但增加了新的數組在位置J明智意味着從上到下,而是我想補充這在位置j collums明智的手段從左到右。在位置j遠離第一個collum –

0
string[] MyArray= new string[3]; 
     MyArray[0] = "Tom"; 
     MyArray[1] = "Canada"; 
     MyArray[2] = "+42-54948354-9";    
DataTable table = new DataTable(); 
      table.Columns.Add("Name"); 
      table.Columns.Add("Address"); 
      table.Columns.Add("CellNo");       
      DataRow drow; 
      drow = table.NewRow(); 
      drow.ItemArray = string.Join(",", arr); 
      table.Rows.Add(drow); 
+0

請仔細閱讀這個問題我已經發布了詳細信息。我想這樣請幫助,如果你能做到,這個方法將幫助很多人解決。因爲我花了很多天從所有sheet的excel文件中讀取所有數據.http://stackoverflow.com/questions/21779543/how-to-retrun-a-single-datatable-from-all-excell-sheet-using-互操作,Excel的天秤座 –

0
public System.Data.DataTable SetOne(string ExcelFilePath) 
{  
     System.Data.DataTable table = new System.Data.DataTable(); 
     Microsoft.Office.Interop.Excel.Application app = new        Microsoft.Office.Interop.Excel.Application(); 
    Microsoft.Office.Interop.Excel.Workbook wb = app.Workbooks.Open(ExcelFilePath, 
     Type.Missing, Type.Missing, Type.Missing, Type.Missing, 
      Type.Missing, Type.Missing, Type.Missing, Type.Missing, 
      Type.Missing, Type.Missing, Type.Missing, Type.Missing, 
      Type.Missing, Type.Missing); 
      int NoOfSheetRows=0; 
    foreach (Worksheet item in app.Worksheets) 
    { 
     string sheetname = item.Name; 
     Worksheet sheet = (Worksheet)wb.Sheets[sheetname]; 

     Range excelRange = sheet.UsedRange; 
     string fileRange = sheet.UsedRange.Address; 
     string filecolums = fileRange.Substring(6, 1); 
     List<string> str = new List<string>(); 
     int cntr = 0; 

     foreach (Microsoft.Office.Interop.Excel.Range row in excelRange.Rows) 
     { 
      int rowNumber = row.Row; 
      string[] A4D4 = this.GetRange("A" + rowNumber + ":" + filecolums + "" + rowNumber + "", sheet); 

      if (rowNumber.Equals(1)) 
      { 
       foreach (var itm in A4D4) 
       { 
        if (table.Columns.Contains(itm)==false) 
        { 
         table.Columns.Add(itm); 
         str.Add(itm); 
         cntr++; 
        } 
        else 
        { 
         table.Columns.Add(itm + ".."); 
         str.Add(itm); 
         cntr++; 
        } 
       } 
      } 
      else 
      { 
       DataRow drow; 
       drow = table.NewRow(); 
       drow.ItemArray = A4D4; 
       for(int i=0;i<A4D4.Length; i++) 
       { 
        drow= table.NewRow(); 
        drow["name"] = A4D4[i]; 
        table.Rows.Add(drow); 
       } 
       //table.Rows.InsertAt(drow, NoOfSheetRows); 
       //table.Rows.Add(drow); // This is Area where the Problem is created the the sheet 2,3,4 and so forth data is inserted to 1st Sheet Columns 
      } 
     } 
     NoOfSheetRows += cntr; 
    } 
    return table; 
} 

public string[] GetRange(string range, Worksheet excelWorksheet) 
{ 
    Microsoft.Office.Interop.Excel.Range workingRangeCells = 
     excelWorksheet.get_Range(range, Type.Missing); 
    System.Array array = (System.Array)workingRangeCells.Cells.Value2; 
    string[] arrayS = array.OfType<object>().Select(o => o.ToString()).ToArray(); 
    return arrayS; 
} 
相關問題