datatable . help needed
-
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
-
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
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
-
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
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