Build a DataTable from Scratch

My friend has asked my how to build a data table manually. E.g. Add a custom rows into the data table …etc.
Here it is an example to build a data table from scratch. in C#
using System.Data;
//First of all you need to create a new table
DataTable tbl = new DataTable();
//then you need to specific the columns that you want to have on that DataTable
tbl.Columns.Add[“Column1”];
tbl.Columns.Add[“Column2”];
tbl.Columns.Add[“Column3”];

//…ETC.
//Then you can start to build your table.
//You need to create a empty row and then put it into the data table
DataRow dr = new DataRow();
dr[“Column1”].Add(“Hello”);
dr[“Column2”].Add(“MyTable”);
dr[“Column1”].Add(“Example”);
//Finally add it into the data table
tbl.Rows.Add(dr);
There it is you just created a Data table with 1 row.
Happy programming

Leave a Reply