How to add multiple columns at once to a DataTable Object using C# and AddRange

28 Feb

So instead of creating 20 DataTable.Columns.Add statements use add range and use a array of columns and some copy pasta magic like the following

DeliveryCompanyInfo.Columns.AddRange(new DataColumn[]
new DataColumn("CompanyName"),
new DataColumn("Name"),
new DataColumn("Phone"),
new DataColumn("Address1"),
new DataColumn("Address2"),
new DataColumn("City"),
new DataColumn("State"),
new DataColumn("Zip"),


Work smarter not harder. If you have to define a table with more then 2 or three columns use this for maintainability, proper disposal, legibility, and productive use of time.

Leave a comment

Posted by on February 28, 2012 in C#


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: