<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Create JMP datatable from C# DataTable variable in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Create-JMP-datatable-from-C-DataTable-variable/m-p/49936#M28403</link>
    <description>&lt;P&gt;Hi Chris,&lt;/P&gt;
&lt;P&gt;Thanks for the update. I didn't think about the&amp;nbsp;&lt;EM&gt;SetDataVector&amp;nbsp;&lt;/EM&gt;method when working on the example. Good catch.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;
&lt;P&gt;It would be nice if this pasting could be done from something other than the windows clipboard. (i.e. a tab delimited string passed from a method).&amp;nbsp; Or a method that can take datatable variable directly and convert to JMP datatable.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If you already have the table in the form of a tab-delimited string, why not just save the string out to a file and open that file in JMP? That would remove reliance on the Windows clipboard.&lt;/P&gt;
&lt;P&gt;Or even better, you could just set a JSL variable to the tab-delimted string and use the Open function to open the table directly from the string. Below is an example of how to do that.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;tabDelimitedStr = "name	age	sex	height	weight
KATIE	12	F	59	95
LOUISE	12	F	61	123
JANE	12	F	55	74";

Open( Char to Blob( tabDelimitedStr ), "text" );&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Basically, you can use a JSL variable instead of the clipboard to pass the string, and the Open function to create your table instead of the "Paste with Column Names" option.&lt;/P&gt;</description>
    <pubDate>Mon, 22 Jan 2018 15:16:58 GMT</pubDate>
    <dc:creator>Justin_Chilton</dc:creator>
    <dc:date>2018-01-22T15:16:58Z</dc:date>
    <item>
      <title>Create JMP datatable from C# DataTable variable</title>
      <link>https://community.jmp.com/t5/Discussions/Create-JMP-datatable-from-C-DataTable-variable/m-p/49392#M28090</link>
      <description>&lt;P&gt;What is the most effecient way of creating a JMP datatable from a C# DataTable variable?&amp;nbsp; I am presently using the windows clipboard to paste all the information into JMP. (C# first filling the clipboard)&amp;nbsp; This works, but Windows clipboard is not reliable and sometimes comes-up empty.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I sent a&amp;nbsp;support ticket&amp;nbsp;a few months back and got this messsage..&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Support Message:&lt;/P&gt;&lt;P&gt;...........................&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;Also, you can use the GetTableHandleFromIndex or GetTableHandleFromName methods to get a C# reference to DataTable automation object, as documented &lt;A href="https://support.sas.com/documentation/onlinedoc/jmp/13.1/AutomationReference.pdf#page=68" target="_blank"&gt;here&lt;/A&gt;.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;...........................&lt;/P&gt;&lt;P&gt;I didn't really understand what this meant, when I read the link and was clueless as what to do?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Basically C#&amp;nbsp; JMP automation code I want to do somethign like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;CreateJMPDatatable (DT)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;where DT is a variable of type DataTable (C#)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I could try nested for loops to loop through columns and rows and have JSL populate cell by cell and create needed columns and rows, (which I will try soon) but wondering if there is a quicker and easier way to achieve the same result?&amp;nbsp;&amp;nbsp; (100,000 rows and 100 columns, so I want something quick and effiecient)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you,&lt;/P&gt;</description>
      <pubDate>Mon, 08 Jan 2018 20:12:41 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Create-JMP-datatable-from-C-DataTable-variable/m-p/49392#M28090</guid>
      <dc:creator>ChrisM_X</dc:creator>
      <dc:date>2018-01-08T20:12:41Z</dc:date>
    </item>
    <item>
      <title>Re: Create JMP datatable from C# DataTable variable</title>
      <link>https://community.jmp.com/t5/Discussions/Create-JMP-datatable-from-C-DataTable-variable/m-p/49425#M28106</link>
      <description>&lt;P&gt;JMP ships with a C# sample program.&amp;nbsp; For JMP 13, this is located in&amp;nbsp;C:\Program Files\SAS\JMP\13\Samples\Automation\Visual CSharp Sample.&amp;nbsp; This will show how to open a data table.&amp;nbsp; The Visual Basic automation sample for DataTable will illustrate the methods for creating a new table and populating it (just instantiate a DataTable variable and use NewColumn).&amp;nbsp; The methods are easily translated to C#.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Brian Corcoran&lt;/P&gt;
&lt;P&gt;JMP Development&lt;/P&gt;</description>
      <pubDate>Tue, 09 Jan 2018 13:13:34 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Create-JMP-datatable-from-C-DataTable-variable/m-p/49425#M28106</guid>
      <dc:creator>briancorcoran</dc:creator>
      <dc:date>2018-01-09T13:13:34Z</dc:date>
    </item>
    <item>
      <title>Re: Create JMP datatable from C# DataTable variable</title>
      <link>https://community.jmp.com/t5/Discussions/Create-JMP-datatable-from-C-DataTable-variable/m-p/49449#M28118</link>
      <description>&lt;P&gt;Hi Chris,&lt;/P&gt;
&lt;P&gt;I believe I was the one who helped you out a few months ago. The snippet you provided from our conversation was in reference to creating a C# DataTable object from a JMP data table, not the other way around. Below is what I said about going from C# to JMP.&lt;/P&gt;
&lt;BLOCKQUOTE&gt;There is not a direct path for converting a C# data table object to a JMP Data Table. Instead, you will need to use JMP Automation methods to create an empty table, loop through columns of the C# table, creating the JMP columns, and then loop through the rows of the C# table, adding the row/column values to the JMP Data Table.&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Here is something I threw together that loops through the columns and rows to create a JMP.DataTable:&lt;/P&gt;
&lt;PRE&gt;class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            // call method to get sample C# data table
            DataTable dt = p.GetTable();

            JMP.Application myJMP = new JMP.Application();
            myJMP.Visible = true;
            myJMP.EnableInteractiveMode(true);

            JMP.DataTable jmpDT;
            jmpDT = myJMP.NewDataTable("Example");
            Object jmpCol;
            
            // add each column to the JMP table
            foreach (DataColumn col in dt.Columns)
            {
                // if its an integer, create numeric column (you may need to check for other Types, such as Decimal or DateTime)
                if (col.DataType == System.Type.GetType("System.Int32"))
                {
                    jmpCol = jmpDT.NewColumn(col.ColumnName, JMP.colDataTypeConstants.dtTypeNumeric, 0, 8);
                }
                else
                {
                    jmpCol = jmpDT.NewColumn(col.ColumnName, JMP.colDataTypeConstants.dtTypeCharacter, 0, 8);
                }
            }
            
            // add rows to the table
            jmpDT.AddRows( (Int16) dt.Rows.Count, 0 );
            int i = 1;
            foreach (DataRow row in dt.Rows)
            {
                foreach (DataColumn col in dt.Columns)
                {
                    jmpDT.GetColumn(col.ColumnName).SetCellVal(i, row[col.ColumnName].ToString());
                }
                i++;
            }

            

        }
        // From:  https://msdn.microsoft.com/en-us/library/9ha04z01(v=vs.110).aspx
        private DataTable GetTable()
        {
            // Create new DataTable.
            DataTable table = new DataTable();

            // Declare DataColumn and DataRow variables.
            DataColumn column;
            DataRow row;

            // Create new DataColumn, set DataType, ColumnName
            // and add to DataTable.    
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.Int32");
            column.ColumnName = "id";
            table.Columns.Add(column);

            // Create second column.
            column = new DataColumn();
            column.DataType = Type.GetType("System.String");
            column.ColumnName = "item";
            table.Columns.Add(column);

            // Create new DataRow objects and add to DataTable.    
            for (int i = 0; i &amp;lt; 10; i++)
            {
                row = table.NewRow();
                row["id"] = i;
                row["item"] = "item " + i;
                table.Rows.Add(row);
            }
            return table;
        }
    }&lt;/PRE&gt;
&lt;P&gt;I hope this is helpful. Let me know if you have any further questions.&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Tue, 09 Jan 2018 21:21:04 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Create-JMP-datatable-from-C-DataTable-variable/m-p/49449#M28118</guid>
      <dc:creator>Justin_Chilton</dc:creator>
      <dc:date>2018-01-09T21:21:04Z</dc:date>
    </item>
    <item>
      <title>Re: Create JMP datatable from C# DataTable variable</title>
      <link>https://community.jmp.com/t5/Discussions/Create-JMP-datatable-from-C-DataTable-variable/m-p/49450#M28119</link>
      <description>&lt;P&gt;Thank you for the detailed response, JMP technical support is second to none!&lt;/P&gt;</description>
      <pubDate>Tue, 09 Jan 2018 21:27:02 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Create-JMP-datatable-from-C-DataTable-variable/m-p/49450#M28119</guid>
      <dc:creator>ChrisM_X</dc:creator>
      <dc:date>2018-01-09T21:27:02Z</dc:date>
    </item>
    <item>
      <title>Re: Create JMP datatable from C# DataTable variable</title>
      <link>https://community.jmp.com/t5/Discussions/Create-JMP-datatable-from-C-DataTable-variable/m-p/49453#M28121</link>
      <description>&lt;BR /&gt;Also, sorry for the question again, you essentially had answered my question some months back, my reading old email skills was not so good yesterday! Thanks again!&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;There is not a direct path for converting a C# data table object to a JMP Data Table. Instead, you will need to use JMP Automation methods to create an empty table, loop through columns of the C# table, creating the JMP columns, and then loop through the rows of the C# table, adding the row/column values to the JMP Data Table.</description>
      <pubDate>Tue, 09 Jan 2018 21:33:01 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Create-JMP-datatable-from-C-DataTable-variable/m-p/49453#M28121</guid>
      <dc:creator>ChrisM_X</dc:creator>
      <dc:date>2018-01-09T21:33:01Z</dc:date>
    </item>
    <item>
      <title>Re: Create JMP datatable from C# DataTable variable</title>
      <link>https://community.jmp.com/t5/Discussions/Create-JMP-datatable-from-C-DataTable-variable/m-p/49455#M28123</link>
      <description>Thanks for the link to the solution I was able to review the code fine in visual studio. BTW JMP automation runs fine in my solution, but the sample solution gave an error. I have JMP 13 and visual studio 2017 installed.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;When I click any button on the GUI, I get this error:&lt;BR /&gt;&lt;BR /&gt;System.BadImageFormatException occurred&lt;BR /&gt;HResult=0x8013101B&lt;BR /&gt;Message=Could not load file or assembly 'Interop.JMP, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.&lt;BR /&gt;Source=&amp;lt;Cannot evaluate the exception source&amp;gt;&lt;BR /&gt;StackTrace:&lt;BR /&gt;&amp;lt;Cannot evaluate the exception stack trace&amp;gt;&lt;BR /&gt;</description>
      <pubDate>Tue, 09 Jan 2018 21:46:02 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Create-JMP-datatable-from-C-DataTable-variable/m-p/49455#M28123</guid>
      <dc:creator>ChrisM_X</dc:creator>
      <dc:date>2018-01-09T21:46:02Z</dc:date>
    </item>
    <item>
      <title>Re: Create JMP datatable from C# DataTable variable</title>
      <link>https://community.jmp.com/t5/Discussions/Create-JMP-datatable-from-C-DataTable-variable/m-p/49747#M28308</link>
      <description>&lt;P&gt;Just an update, &lt;A href="https://community.jmp.com/t5/user/viewprofilepage/user-id/1855" target="_blank"&gt;Justin_Chilton&lt;/A&gt;, while your solutions works, it was extremely slow for very large tables (and turned out to be inadequate for my needs)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It is much faster to use&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;JmpDT.GetColumn(ColName).SetDataVector (which populates 1 column at a time)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;instead of&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;jmpDT.GetColumn(col.ColumnName).SetCellVal&lt;/PRE&gt;&lt;P&gt;Which only populates 1 cell at a time&lt;/P&gt;&lt;P&gt;So you just need code to convert data columns in datatables into array to feed into the SetDataVector method&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;example:&amp;nbsp; string[] ColArray = Summary.AsEnumerable().Select(rw =&amp;gt; rw.Field&amp;lt;string&amp;gt;(ColName)).ToArray();&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So with this approach,you only need to loop through columns once:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;sample code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;for (int i = 0; i &amp;lt; Summary.Columns.Count; i++)&lt;BR /&gt;{&lt;BR /&gt;string ColName = Summary.Columns[i].ColumnName;&lt;/P&gt;&lt;P&gt;var datatype = Summary.Columns[i].DataType;&lt;/P&gt;&lt;P&gt;if (datatype==typeof(string))&lt;BR /&gt;{&lt;BR /&gt;&lt;BR /&gt;string[] ColArray = Summary.AsEnumerable().Select(rw =&amp;gt; rw.Field&amp;lt;string&amp;gt;(ColName)).ToArray();&lt;BR /&gt;JmpDT.GetColumn(ColName).SetDataVector(ColArray);&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;}&lt;BR /&gt;else if (datatype == typeof(double))&lt;BR /&gt;{&lt;BR /&gt;double[] ColArray = Summary.AsEnumerable().Select(rw =&amp;gt; rw.Field&amp;lt;double&amp;gt;(ColName)).ToArray();&lt;BR /&gt;jmpDT.GetColumn(ColName).SetDataVector(ColArray);&lt;BR /&gt;}&lt;BR /&gt;else if (datatype == typeof(int))&lt;BR /&gt;{&lt;BR /&gt;int[] ColArray = Summary.AsEnumerable().Select(rw =&amp;gt; rw.Field&amp;lt;int&amp;gt;(ColName)).ToArray();&lt;BR /&gt;jmpDT.GetColumn(ColName).SetDataVector(ColArray);&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;else if (datatype == typeof(float))&lt;BR /&gt;{&lt;BR /&gt;float[] ColArray = Summary.AsEnumerable().Select(rw =&amp;gt; rw.Field&amp;lt;float&amp;gt;(ColName)).ToArray();&lt;BR /&gt;jmpDT.GetColumn(ColName).SetDataVector(ColArray);&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;While this solution works (and is orders of magnitude faster than using "SetCellVal" method, it still is almost twice as slow as pasting a tab delimted string from windows clipboard for my application.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I did time trials: ~9000 rows and 175 columns&lt;/P&gt;&lt;P&gt;pasting clipboard ~2.5s, while technique above ~4.5s&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So I would like to request in the future a more direct method to bring data into JMP from C# datatables.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would use the windows clipboard, but even&amp;nbsp; with error trapping, the Windows clipboard can be flakey and unreiliable on some people PC's hence would not like to use that technique.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;FYI WIndows clipboard would be populated with a tab delimited string, then use this code to get data into JMP.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;command = "dts &amp;lt;&amp;lt; Bring Window To Front &amp;lt;&amp;lt; Clear Column Selection &amp;lt;&amp;lt; clear select; dts=currentdatatable();";&lt;BR /&gt;myJMP.RunCommand(command);&lt;BR /&gt;command = "main menu(\"Paste With Column Names\");";&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;myJMP.RunCommand(command);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It would be nice if this pasting could be done from something other than the windows clipboard. (i.e. a tab delimited string passed from a method).&amp;nbsp; Or a method that can take datatable variable directly and convert to JMP datatable.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your consideration,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Chris&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Jan 2018 11:22:01 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Create-JMP-datatable-from-C-DataTable-variable/m-p/49747#M28308</guid>
      <dc:creator>ChrisM_X</dc:creator>
      <dc:date>2018-01-17T11:22:01Z</dc:date>
    </item>
    <item>
      <title>Re: Create JMP datatable from C# DataTable variable</title>
      <link>https://community.jmp.com/t5/Discussions/Create-JMP-datatable-from-C-DataTable-variable/m-p/49936#M28403</link>
      <description>&lt;P&gt;Hi Chris,&lt;/P&gt;
&lt;P&gt;Thanks for the update. I didn't think about the&amp;nbsp;&lt;EM&gt;SetDataVector&amp;nbsp;&lt;/EM&gt;method when working on the example. Good catch.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;
&lt;P&gt;It would be nice if this pasting could be done from something other than the windows clipboard. (i.e. a tab delimited string passed from a method).&amp;nbsp; Or a method that can take datatable variable directly and convert to JMP datatable.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If you already have the table in the form of a tab-delimited string, why not just save the string out to a file and open that file in JMP? That would remove reliance on the Windows clipboard.&lt;/P&gt;
&lt;P&gt;Or even better, you could just set a JSL variable to the tab-delimted string and use the Open function to open the table directly from the string. Below is an example of how to do that.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;tabDelimitedStr = "name	age	sex	height	weight
KATIE	12	F	59	95
LOUISE	12	F	61	123
JANE	12	F	55	74";

Open( Char to Blob( tabDelimitedStr ), "text" );&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Basically, you can use a JSL variable instead of the clipboard to pass the string, and the Open function to create your table instead of the "Paste with Column Names" option.&lt;/P&gt;</description>
      <pubDate>Mon, 22 Jan 2018 15:16:58 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Create-JMP-datatable-from-C-DataTable-variable/m-p/49936#M28403</guid>
      <dc:creator>Justin_Chilton</dc:creator>
      <dc:date>2018-01-22T15:16:58Z</dc:date>
    </item>
  </channel>
</rss>

