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. How can convert a lit<> to a DataTable?

How can convert a lit<> to a DataTable?

Scheduled Pinned Locked Moved C#
question
5 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.
  • D Offline
    D Offline
    devenv exe
    wrote on last edited by
    #1

    i have the code below and want to convert the results in var rows to a DataTable

    var rows = new List<Row>();
    var sr = new StreamReader(dirCSV + fileNevCSV);
    while (!sr.EndOfStream)
    {
    string s = sr.ReadLine();
    if (!String.IsNullOrEmpty(s.Trim()))
    {
    rows.Add(new Row(s));
    }
    }
    sr.Close();

    "Coming soon"

    W L D R 4 Replies Last reply
    0
    • D devenv exe

      i have the code below and want to convert the results in var rows to a DataTable

      var rows = new List<Row>();
      var sr = new StreamReader(dirCSV + fileNevCSV);
      while (!sr.EndOfStream)
      {
      string s = sr.ReadLine();
      if (!String.IsNullOrEmpty(s.Trim()))
      {
      rows.Add(new Row(s));
      }
      }
      sr.Close();

      "Coming soon"

      W Offline
      W Offline
      Wayne Gaylard
      wrote on last edited by
      #2

      It would obviously depend on what sort of object is your Row class. Maybe your best bet would be to create the DataTable first and then add each DataRow while reading the csv files, instead of creating the List.

      When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman

      1 Reply Last reply
      0
      • D devenv exe

        i have the code below and want to convert the results in var rows to a DataTable

        var rows = new List<Row>();
        var sr = new StreamReader(dirCSV + fileNevCSV);
        while (!sr.EndOfStream)
        {
        string s = sr.ReadLine();
        if (!String.IsNullOrEmpty(s.Trim()))
        {
        rows.Add(new Row(s));
        }
        }
        sr.Close();

        "Coming soon"

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Take a look at Using OleDb to Import Text Files (tab, CSV, custom)[^], it may be just what you need.

        One of these days I'm going to think of a really clever signature.

        1 Reply Last reply
        0
        • D devenv exe

          i have the code below and want to convert the results in var rows to a DataTable

          var rows = new List<Row>();
          var sr = new StreamReader(dirCSV + fileNevCSV);
          while (!sr.EndOfStream)
          {
          string s = sr.ReadLine();
          if (!String.IsNullOrEmpty(s.Trim()))
          {
          rows.Add(new Row(s));
          }
          }
          sr.Close();

          "Coming soon"

          D Offline
          D Offline
          devenv exe
          wrote on last edited by
          #4

          Passing rows to the function below got it working.

          public static DataTable ToDataTable(this IList data)
          {
          PropertyDescriptorCollection props =
          TypeDescriptor.GetProperties(typeof(T));
          DataTable table = new DataTable();
          for(int i = 0 ; i < props.Count ; i++)
          {
          PropertyDescriptor prop = props[i];
          table.Columns.Add(prop.Name, prop.PropertyType);
          }
          object[] values = new object[props.Count];
          foreach (T item in data)
          {
          for (int i = 0; i < values.Length; i++)
          {
          values[i] = props[i].GetValue(item);
          }
          table.Rows.Add(values);
          }
          return table;
          }

          "Coming soon"

          1 Reply Last reply
          0
          • D devenv exe

            i have the code below and want to convert the results in var rows to a DataTable

            var rows = new List<Row>();
            var sr = new StreamReader(dirCSV + fileNevCSV);
            while (!sr.EndOfStream)
            {
            string s = sr.ReadLine();
            if (!String.IsNullOrEmpty(s.Trim()))
            {
            rows.Add(new Row(s));
            }
            }
            sr.Close();

            "Coming soon"

            R Offline
            R Offline
            Ramesh Muthiah
            wrote on last edited by
            #5

            Best you would create a DataRow and add the content, then add it to IList collection. it will give a new extension method CopyToDataTable() which you can easily make conversion. IList iRows = new List(); DataTable iTable = iRows.CopyToDataTable();

            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