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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. datatable . help needed

datatable . help needed

Scheduled Pinned Locked Moved C#
help
3 Posts 3 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.
  • S Offline
    S Offline
    superselector
    wrote on last edited by
    #1

    i have a datatable with 3 columns Class, Student details, School Name DataTable dt = new DataTable(); dt.Columns.Add("Class", typeof(System.String)); dt.Columns.Add("Student Details", typeof(System.String)); dt.Columns.Add("School Name", typeof(System.String)); dt.Rows.Add("12", "binod 16 Maths","X"); dt.Rows.Add("12", "Alex 17 History", "Y"); dt.Rows.Add("12", "Sam 18 Biology", "Z"); I want to convert this datatable into this below new datatable where i have splitted the "Student Deatils" column into 3 columns and added the values of this column separted by space in new columsn dtNew.Columns.Add("Class", typeof(System.String)); dtNew.Columns.Add("Name", typeof(System.String)); dtNew.Columns.Add("Age", typeof(System.String)); dtNew.Columns.Add("Subject", typeof(System.String)); dtNew.Columns.Add("School Name", typeof(System.String)); Class Name Age Subject School Name 12 binod 16 maths X 12 Alex 17 History Y 12 Sam 18 Biology Z

    A A 2 Replies Last reply
    0
    • S superselector

      i have a datatable with 3 columns Class, Student details, School Name DataTable dt = new DataTable(); dt.Columns.Add("Class", typeof(System.String)); dt.Columns.Add("Student Details", typeof(System.String)); dt.Columns.Add("School Name", typeof(System.String)); dt.Rows.Add("12", "binod 16 Maths","X"); dt.Rows.Add("12", "Alex 17 History", "Y"); dt.Rows.Add("12", "Sam 18 Biology", "Z"); I want to convert this datatable into this below new datatable where i have splitted the "Student Deatils" column into 3 columns and added the values of this column separted by space in new columsn dtNew.Columns.Add("Class", typeof(System.String)); dtNew.Columns.Add("Name", typeof(System.String)); dtNew.Columns.Add("Age", typeof(System.String)); dtNew.Columns.Add("Subject", typeof(System.String)); dtNew.Columns.Add("School Name", typeof(System.String)); Class Name Age Subject School Name 12 binod 16 maths X 12 Alex 17 History Y 12 Sam 18 Biology Z

      A Offline
      A Offline
      Abhinav S
      wrote on last edited by
      #2

      You have got the general idea right. Are you facing some issue? The Copy[^] method might give you some ideas well. This thread[^] might be useful to you as well.

      Apps - Color Analyzer | Arctic | XKCD | Sound Meter | Speed Dial

      1 Reply Last reply
      0
      • S superselector

        i have a datatable with 3 columns Class, Student details, School Name DataTable dt = new DataTable(); dt.Columns.Add("Class", typeof(System.String)); dt.Columns.Add("Student Details", typeof(System.String)); dt.Columns.Add("School Name", typeof(System.String)); dt.Rows.Add("12", "binod 16 Maths","X"); dt.Rows.Add("12", "Alex 17 History", "Y"); dt.Rows.Add("12", "Sam 18 Biology", "Z"); I want to convert this datatable into this below new datatable where i have splitted the "Student Deatils" column into 3 columns and added the values of this column separted by space in new columsn dtNew.Columns.Add("Class", typeof(System.String)); dtNew.Columns.Add("Name", typeof(System.String)); dtNew.Columns.Add("Age", typeof(System.String)); dtNew.Columns.Add("Subject", typeof(System.String)); dtNew.Columns.Add("School Name", typeof(System.String)); Class Name Age Subject School Name 12 binod 16 maths X 12 Alex 17 History Y 12 Sam 18 Biology Z

        A Offline
        A Offline
        Andy_L_J
        wrote on last edited by
        #3

        Try

        System.Data.DataTable dt = new DataTable();
        dt.Columns.Add("Class", typeof(System.String));
        dt.Columns.Add("Student Details", typeof(System.String));
        dt.Columns.Add("School Name", typeof(System.String));
        dt.Rows.Add("12", "binod 16 Maths", "X");
        dt.Rows.Add("12", "Alex 17 History", "Y");
        dt.Rows.Add("12", "Sam 18 Biology", "Z");

        DataTable dtNew = new DataTable();
        dtNew.Columns.Add("Class", typeof(System.String));
        dtNew.Columns.Add("Name", typeof(System.String));
        dtNew.Columns.Add("Age", typeof(System.String));
        dtNew.Columns.Add("Subject", typeof(System.String));
        dtNew.Columns.Add("School Name", typeof(System.String));

        foreach (DataRow r in dt.Rows)
        {
        string details = (string)r[1];
        string[] row = details.Split(' ');
        dtNew.Rows.Add(r[0], row[0], row[1], row[2], r[2]);
        }

        foreach (DataRow r in dtNew.Rows)
        {
        Console.WriteLine(r[0] + " " + r[1] + " " + r[2] + " " + r[3] + " " + r[4]);
        }

        Console.ReadKey();

        I don't speak Idiot - please talk slowly and clearly "I have sexdaily. I mean dyslexia. Fcuk!" Driven to the arms of Heineken by the wife

        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