How to

manually create and fill DataTable

Published: 14. December 2008 | Updated: 26. October 2011
License: Microsoft Public License (MS-PL)
Categories: Databases, Winforms
Tags: C# SQL Webforms Winforms WPF
Was this snippet helpful for you? YESYES / NONO
Get Analyzant!

Check your entire website from code to content in minutes with Analyzant, the free analysis suite.

Free download at analyzant.com

Import namespace

using System.Data;

Code

DataTable table = new DataTable();

DataColumn column;

column = table.Columns.Add();
column.ColumnName = "id";
column.DataType = typeof(Guid);

column = table.Columns.Add();
column.ColumnName = "name";
column.DataType = typeof(string);

column = table.Columns.Add();
column.ColumnName = "age";
column.DataType = typeof(int);

DataRow row;

row = table.NewRow();
row["id"] = Guid.NewGuid();
row["name"] = "Richard";
row["age"] = 45;
table.Rows.Add(row);

row = table.NewRow();
row["id"] = Guid.NewGuid();
row["name"] = "Denisa";
row["age"] = 38;
table.Rows.Add(row);
Send us feedback about this snippet »



Related Snippets: