locked
Converting array to datatable

    Question

  • Hey everyone need a little help converting an array to a datatable. Problem I have is I want to be able to select a set of rows from a datatable and plan to use datatable.select unfortunitly it returns an array instead of a datatable (not sure the logic behind that one) but I need the selected rows to be converted back into a datatable.

    Any insight into how to convert an array to a datatable or a more efficient method would be greatly appreciated.
    Monday, April 09, 2007 2:45 PM

Answers

  •  

    Try to use a DataView passing it the select criteria and then call ToTable()  wich returns a generic DataTable.

     

    C#

    DataTable dt = new DataTable("Table");

    dt.Columns.Add("ID", typeof(int));

    dt.Columns.Add("Name", typeof(string));

    dt.Rows.Add(1, "One");

    dt.Rows.Add(2, "Two");

    DataView dataView = new DataView(dt, "ID = 1", "", DataViewRowState.CurrentRows);

    DataTable newTable = dataView.ToTable();

    Console.WriteLine("Table Name: {1}{0}Rows: {2}{0}", Environment.NewLine, newTable.TableName, newTable.Rows.Count);

     

    Visual Basic

    Dim dt As New DataTable("Table")

    dt.Columns.Add("ID", GetType(Integer))

    dt.Columns.Add("Name", GetType(String))

    dt.Rows.Add(1, "One")

    dt.Rows.Add(2, "Two")

    Dim dataView As New DataView(dt, "ID = 1", "", DataViewRowState.CurrentRows)

    Dim newTable As DataTable = dataView.ToTable()

    Console.WriteLine("Table Name: {1}{0}Rows: {2}{0}", Environment.NewLine, newTable.TableName, newTable.Rows.Count)

     

    Monday, April 09, 2007 3:18 PM

All replies

  • Not certain of this but I think it returns an array of datatable rows of type DataTableRow, so the logic checks out, because multible rows meet the requierments of your query, you could do a couple of things, you could loop through the array and add the rows to a new datatable, or you could use the GetData() Function of the DataTable which returns a DataTable.
    Monday, April 09, 2007 3:06 PM
  • Hmm, ya shouldn't be too hard to do it through a loop if it returns an array of tablerows, however what's this GetData() function I can't seem to find it as a function of datatable, i.e. datatable.getdata().
    Monday, April 09, 2007 3:14 PM
  •  

    Try to use a DataView passing it the select criteria and then call ToTable()  wich returns a generic DataTable.

     

    C#

    DataTable dt = new DataTable("Table");

    dt.Columns.Add("ID", typeof(int));

    dt.Columns.Add("Name", typeof(string));

    dt.Rows.Add(1, "One");

    dt.Rows.Add(2, "Two");

    DataView dataView = new DataView(dt, "ID = 1", "", DataViewRowState.CurrentRows);

    DataTable newTable = dataView.ToTable();

    Console.WriteLine("Table Name: {1}{0}Rows: {2}{0}", Environment.NewLine, newTable.TableName, newTable.Rows.Count);

     

    Visual Basic

    Dim dt As New DataTable("Table")

    dt.Columns.Add("ID", GetType(Integer))

    dt.Columns.Add("Name", GetType(String))

    dt.Rows.Add(1, "One")

    dt.Rows.Add(2, "Two")

    Dim dataView As New DataView(dt, "ID = 1", "", DataViewRowState.CurrentRows)

    Dim newTable As DataTable = dataView.ToTable()

    Console.WriteLine("Table Name: {1}{0}Rows: {2}{0}", Environment.NewLine, newTable.TableName, newTable.Rows.Count)

     

    Monday, April 09, 2007 3:18 PM
  • not sure if my last post actually posted... MSDN forums are being kind of strange for me so I'll sort of repost, curious if you can elaborate on this getdata() function I can't seem to make it work i.e. datatable.getdata()
    Monday, April 09, 2007 3:26 PM
  •  

    I don't know what happened with my previous attempt to post this so I'll do it again. 

     

    Initialize a DataView with a valid criteria and call ToTable. However, the table returned is a generic DataTable.

     

    Visual Basic

    Dim dt As New DataTable("Table")

    dt.Columns.Add("ID", GetType(Integer))

    dt.Columns.Add("Name", GetType(String))

    dt.Rows.Add(1, "One")

    dt.Rows.Add(2, "Two")

    Dim dataView As New DataView(dt, "ID = 1", "", DataViewRowState.CurrentRows)

    Dim newTable As DataTable = dataView.ToTable()

    Console.WriteLine("Table Name: {1}{0}Rows: {2}{0}", Environment.NewLine, newTable.TableName, newTable.Rows.Count)

     

    Monday, April 09, 2007 3:27 PM
  • lol looks like the MSDN forums are having some issues, either way I appreciate all the help I've went with Cristian_t solution. Works like a dream and I've managed to reduce it to 2 lines of code so thank you very much.
    Monday, April 09, 2007 4:19 PM
  • Just for your info, I think the GetData() function which is a query is only generated if you use the DataSet Designer, I apologize for the confusion.
    Monday, April 09, 2007 9:28 PM