Select only necessary columns from the existing Datatable

This is help to filter the necessary columns from the existing datatable into new datatable in visual studio.Net

For selecting only the necessary columns from the n number of columns on another datatable by using Merge option of the datatable.we can ommit the unwanted columns through MissingSchemaAction.Ignore

For example,
Let existingTable as existing datatable with columns named SNo,Name,Sub1,Sub2,Sub3,Total.But we need only Sno,Name,Total alone into a separate datatable.For this,just simply do the following,

DataTable copiedTable = new DataTable();
copiedTable.Columns.Add("SNo", Type.GetType("System.Int32"));
copiedTable.Columns.Add("Name", Type.GetType("System.varchar"));
copiedTable.Columns.Add("Total", Type.GetType("System.Single"));
copiedTable.Merge(existingTable, true, MissingSchemaAction.Ignore);

I hope,this will reduce your overburden.



No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: