Shakir.iti
Shakir.iti

Reputation: 103

How to Add ItemArray to DataRow at a specific Location in DataTable

I have an array "MyArray" and want this array to add to a datatable at Position spcified .The Second last row show where i want to assign the itemarray as at position 3 rows Forward. The same process i did for a single string like datarow.insertAt[i] its working fine but when i am inserting a bunch of array at a specific location then its not working .

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

Upvotes: 1

Views: 12613

Answers (3)

Aftab Ahmed
Aftab Ahmed

Reputation: 1737

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;
}

Upvotes: 0

Kashif
Kashif

Reputation: 14440

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

To add row at specific index you can use InsertAt But if there is no row in table then you can't insert it at position 2 (index 1) because there is no row at position 1 for example following will work:

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

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

Upvotes: 2

Aftab Ahmed
Aftab Ahmed

Reputation: 1737

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

Upvotes: 0

Related Questions