c# - How to add multiple columns at once to a DataTable

Add multiple columns to a DataTable at once
The DataTable class represents one table of in-memory data. The DataTable objects are conditionally case-sensitive. To create a DataTable programmatically the asp.net developers must first define its schema by adding DataColumn objects to the DataColumnCollection. To add rows to a DataTable, the developers must first use the NewRow() method to return a new DataRow object. The DataTable also contains a collection of Constraint objects that can be used to ensure the integrity of the data.

The following asp.net c# tutorial code demonstrates how we can add multiple columns to a DataTable at once. So we have to insert multiple DataColumn objects into a DataTable instance at the same time.

Here we used the DataTable class Columns property to get its columns collection as a DataColumnCollection object. Then we used DataColumnCollection’s AddRange() method to insert multiple columns into the DataTable instance at the same time.

The DataColumnCollection class represents a collection of DataColumn objects for a DataTable. The DataColumnCollection class AddRanger(DataColumn[]) method copies the elements of the specified DataColumn array to the end of the collection. The AddRange(System.Data.DataColumn[] columns) method has a parameter named columns.

The columns parameter is an array of DataColumn objects to add to the collection. So finally, the asp.net c# developers can pass an array of DataColumn objects to the AddRange() method to add multiple columns to the DataTable at once.
AddMultipleDataColumnToDataTable.aspx

<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Data" %>

<!DOCTYPE html>
<script runat="server">
    void Button1_Click(object sender, System.EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.TableName = "Books";
        
        DataColumn dc = new DataColumn();
        dc.ColumnName = "BookID";
        dc.DataType = typeof(int);
        //this line add single DataColumn to DataTable
        dt.Columns.Add(dc);

        DataColumn dc2 = new DataColumn();
        dc2.ColumnName = "BookName";
        dc2.DataType = typeof(string);

        DataColumn dc3 = new DataColumn();
        dc3.ColumnName = "BookPrice";
        dc3.DataType = typeof(decimal);
        
        //this line add two DataColumn to DataTable at a time
        dt.Columns.AddRange(new DataColumn[] {dc2,dc3});

        dt.Rows.Add(new object[] { 1,"asp.net ebook",4 });
        dt.Rows.Add(new object[] { 2, "ajax ebook",5.05 });

        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Using AddRange() method - How to add multiple DataColumn to a DataTable programmatically in ado.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:DarkBlue; font-style:italic;">
            Using AddRange() method - How to add multiple
            <br /> DataColumn to a DataTable programmatically in ado.net
        </h2>
        <hr width="425" align="left" color="CornFlowerBlue" />
        <asp:GridView 
            ID="GridView1"
            runat="server"
            BorderColor="LawnGreen"
            HeaderStyle-BackColor="SeaGreen"
            ForeColor="Snow"
            Width="400"
            >
            <RowStyle BackColor="Snow" ForeColor="DarkSeaGreen" />
        </asp:GridView>
        <br />
        <asp:Button 
            ID="Button1"
            runat="server"
            OnClick="Button1_Click"
            Text="Populate GridView"
            Height="45"
            Font-Bold="true"
            ForeColor="DarkGreen"
            />
    </div>
    </form>
</body>
</html>
More c# examples