Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Quick way to add 1D array to Multidimensional array

Quick way to add 1D array to Multidimensional array

Scheduled Pinned Locked Moved C#
helpdata-structurestutorial
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RYU
    wrote on last edited by
    #1

    Hi, I am trying to find a quick way to add 1 dimensional array into multidimensional array. For example: object[] oneDArray1 = new object[10]; object[] oneDArray2 = new object[10]; object[] oneDArray3 = new object[10]; Then, I need to add this array into 2D Multidimensional array. object[,] twoDArray = new object[3, 10]; The reason I need to do this is because I have a DataSet and I need to convert this DataSet to Excel Application. Currently, I do the following: oRng.Value2 = oDataTable.Rows[iRowIdx].ItemArray This code work if the Range is within single row. Now, if I have 1000 rows, this is process is very slow. It takes about 20 seconds to loop through the range and set the rows. Alternatively, I could set the value of the range at one go. For example I want to set the value from Row #1 Column #1 to Row #1000 Column #1000, I must do object[,] 2DMultiDimensionalArray = new object[1000, 1000]; oRng = oSheet.get_Range("A1", "ALL1000"); oRng.Value2 = 2DMultiDimensionalArray; In order to set the excel value at one go, I MUST convert each DataRow.ItemArray into MultiDimensional array. I need to add Single Dimensional Array into MultiDimensional array (NOT jagged array because I have try it and it throw an error).

    An unhandled exception of type 'System.Runtime.InteropServices.SafeArrayTypeMismatchException' occurred in mscorlib.dll

    Additional information: Specified array was not of the expected type.

    Any help is greatly appreciated! Thanks :)

    E S 2 Replies Last reply
    0
    • R RYU

      Hi, I am trying to find a quick way to add 1 dimensional array into multidimensional array. For example: object[] oneDArray1 = new object[10]; object[] oneDArray2 = new object[10]; object[] oneDArray3 = new object[10]; Then, I need to add this array into 2D Multidimensional array. object[,] twoDArray = new object[3, 10]; The reason I need to do this is because I have a DataSet and I need to convert this DataSet to Excel Application. Currently, I do the following: oRng.Value2 = oDataTable.Rows[iRowIdx].ItemArray This code work if the Range is within single row. Now, if I have 1000 rows, this is process is very slow. It takes about 20 seconds to loop through the range and set the rows. Alternatively, I could set the value of the range at one go. For example I want to set the value from Row #1 Column #1 to Row #1000 Column #1000, I must do object[,] 2DMultiDimensionalArray = new object[1000, 1000]; oRng = oSheet.get_Range("A1", "ALL1000"); oRng.Value2 = 2DMultiDimensionalArray; In order to set the excel value at one go, I MUST convert each DataRow.ItemArray into MultiDimensional array. I need to add Single Dimensional Array into MultiDimensional array (NOT jagged array because I have try it and it throw an error).

      An unhandled exception of type 'System.Runtime.InteropServices.SafeArrayTypeMismatchException' occurred in mscorlib.dll

      Additional information: Specified array was not of the expected type.

      Any help is greatly appreciated! Thanks :)

      E Offline
      E Offline
      Elizma
      wrote on last edited by
      #2

      Do you want to export a DataTable to Excel? I am asking, because I would then suggest to use instead of arrays two loops. An outer-loop to do your rows and a inner-loop to do your columns. Much faster than arrays. Regards, Elizma

      R 1 Reply Last reply
      0
      • R RYU

        Hi, I am trying to find a quick way to add 1 dimensional array into multidimensional array. For example: object[] oneDArray1 = new object[10]; object[] oneDArray2 = new object[10]; object[] oneDArray3 = new object[10]; Then, I need to add this array into 2D Multidimensional array. object[,] twoDArray = new object[3, 10]; The reason I need to do this is because I have a DataSet and I need to convert this DataSet to Excel Application. Currently, I do the following: oRng.Value2 = oDataTable.Rows[iRowIdx].ItemArray This code work if the Range is within single row. Now, if I have 1000 rows, this is process is very slow. It takes about 20 seconds to loop through the range and set the rows. Alternatively, I could set the value of the range at one go. For example I want to set the value from Row #1 Column #1 to Row #1000 Column #1000, I must do object[,] 2DMultiDimensionalArray = new object[1000, 1000]; oRng = oSheet.get_Range("A1", "ALL1000"); oRng.Value2 = 2DMultiDimensionalArray; In order to set the excel value at one go, I MUST convert each DataRow.ItemArray into MultiDimensional array. I need to add Single Dimensional Array into MultiDimensional array (NOT jagged array because I have try it and it throw an error).

        An unhandled exception of type 'System.Runtime.InteropServices.SafeArrayTypeMismatchException' occurred in mscorlib.dll

        Additional information: Specified array was not of the expected type.

        Any help is greatly appreciated! Thanks :)

        S Offline
        S Offline
        Sathesh Sakthivel
        wrote on last edited by
        #3

        Hope this[^] article will give an idea about the arrays.

        Regards, Satips.:rose: Don't walk in front of me, I may not follow; Don't walk behind me, I may not lead; Walk beside me, and just be my friend. - Albert Camus

        R J 2 Replies Last reply
        0
        • E Elizma

          Do you want to export a DataTable to Excel? I am asking, because I would then suggest to use instead of arrays two loops. An outer-loop to do your rows and a inner-loop to do your columns. Much faster than arrays. Regards, Elizma

          R Offline
          R Offline
          RYU
          wrote on last edited by
          #4

          Hi Elizma, Yes, I am indeed exporting DataTable to Excel. Doing loop for each row and each column will be quick if the number of columns aren't big. Here, I am talking about about 1000+ rows by 1000+ columns. Currently, if I do

          for (int iRow = 0; iRow < dt.Rows.Count; iRow++)
          {
          oRng = oSht.get_Range("A" + iRow.ToString(), strLastCol + iRow.ToString());
          oRng.Value2 = (System.Array)dt.Rows[iRow].ItemArray;
          }

          This code is about 30x faster than nested loop. In my PC this loop takes about 20 seconds where as nested loop (to set each cell) takes about 10 minutes. However, if you set directly to MultiDimensional Array, it takes less than 1 second (and probably few seconds to add the 1D array into Multidimensional array). I am starting to think that it is impossible to add 1D array to Multidimensional array without nested loop :( Any other idea to speed up this process? Thanks for any input/help. Cheers :)

          1 Reply Last reply
          0
          • S Sathesh Sakthivel

            Hope this[^] article will give an idea about the arrays.

            Regards, Satips.:rose: Don't walk in front of me, I may not follow; Don't walk behind me, I may not lead; Walk beside me, and just be my friend. - Albert Camus

            R Offline
            R Offline
            RYU
            wrote on last edited by
            #5

            Hi Satips, Thanks for the link. But it doesn't answer my question :(

            1 Reply Last reply
            0
            • S Sathesh Sakthivel

              Hope this[^] article will give an idea about the arrays.

              Regards, Satips.:rose: Don't walk in front of me, I may not follow; Don't walk behind me, I may not lead; Walk beside me, and just be my friend. - Albert Camus

              J Offline
              J Offline
              J4amieC
              wrote on last edited by
              #6

              Satips do you intentionally just answer questions badly?

              --- How to get answers to your questions[^]

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups