Home
C#
DataColumn Example: Columns.Add
Updated Sep 7, 2023
Dot Net Perls
DataColumn. This class designates columns in DataTables. It specifies the name and type of certain columns in the table. We often use the typeof operator with DataColumn.
DataTable
typeof
The DataColumn type can be instantiated through the Columns.Add method or through the DataColumn constructor itself. It is used alongside DataTable and DataRow.
Constructor
An example. We construct a DataTable using DataColumns. The example adds some rows to the DataTable based on the types and count of the columns specified by the DataColumn references.
DataRow
Detail We add DataColumns with Columns.Add. Internally, this overload of the Add method just constructs a new DataColumn.
Overload
Detail Next the program shows the DataColumn constructor which receives 2 parameters.
Tip You can pass an actual DataColumn instance reference to the Add method on the Columns collection.
Then We use a foreach-loop. It accesses the Columns property and loops over this with a DataColumn iteration variable.
foreach
using System; using System.Data; class Program { static void Main() { // // Construct the DataTable. // DataTable table = GetTable(); // // Loop over the column headers in the DataTable. // foreach (DataColumn column in table.Columns) { Console.WriteLine("{0} = {1}", column, column.DataType); } } /// <summary> /// Generates a DataTable with four columns. /// </summary> static DataTable GetTable() { // // Here we create a DataTable and add columns to it. // DataTable table = new DataTable(); table.Columns.Add("Dosage", typeof(int)); table.Columns.Add("Medication", typeof(string)); table.Columns.Add("Patient", typeof(string)); // // Add another column to the data table in a different way. // DataColumn column = new DataColumn("Appointment", typeof(DateTime)); table.Columns.Add(column); // // Here we add some DataRows. // Note that the row parameters must match the order and types of the columns. // table.Rows.Add(21, "Combivent", "Janet", DateTime.Now); table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now); return table; } }
Dosage = System.Int32 Medication = System.String Patient = System.String Appointment = System.DateTime
Internals. DataColumns can be accessed by their string names in the indexer on the DataColumnCollection. Internally, these DataColumn references are stored in a Hashtable.
Hashtable
Note The indexer just uses an internal Hashtable field to look up the references themselves.
Indexer
A discussion. The DataTable, DataColumn and DataRow classes are in-memory relational database representations. Relational databases such as SQL Server store data in a structured way.
However There is a conflict when translating tabular data to in-memory object models and representations.
And The DataColumn type is one way you can represent the data in memory without constantly using the database.
A summary. We looked at the DataColumn class. This type acts as a template for the in-memory relational database abstraction specified by the DataTable type.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
No updates found for this page.
Home
Changes
© 2007-2025 Sam Allen