c# - How to set all the values for a DataRow through an array

DataRow ItemArray Property
.Net framework's DataRow class represents a row of data in a DataTable. DataRow class exists in System.Data namespace.

DataRow.ItemArray property allow us to get or set all the values for this row through an array. ItemArray property value type is System.Object[] which represents an array of type Object.

if we use ItemArray property to set a row values, the array must have the same size and ordering as the DataTable's column collection. passing null in ItemArray indicates that no value was specified.

DataRow ItemArray property throw ArgumentException exception, if the array is larger than the table columns number. it throw InvalidCastException, if a value in the array does not match its DataType in its respective DataColumn. property throw ConstraintException, if an edit broke a constraint.

it throw ReadOnlyException, if an edit tried to change the value of a read-only column. ItemArray property also throw NoNullAllowedException and DeletedRowInaccessibleException. developers can generate exceptions in the ColumnChanging event or RowChanging event.

the following ado.net c# example code demonstrate us how can we use DataRow ItemArray property in an asp.net application.
DataRowItemArrayProperty.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 dc1 = new DataColumn();
        dc1.ColumnName = "BookID";
        dc1.DataType = typeof(int);
        dc1.AllowDBNull = false;
        dc1.Unique = true;

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

        DataColumn dc3 = new DataColumn();
        dc3.ColumnName = "Author";
        dc3.DataType = typeof(string);
        
        dt.Columns.AddRange(new DataColumn[] { dc1,dc2,dc3 });

        dt.Rows.Add(new object[] { 1, "Network Your Computers & Devices Step by Step", "Ciprian Adrian Rusen" });
        dt.Rows.Add(new object[] { 2, "Windows® Phone 7 Plain & Simple", "Michael Stroh" });

        GridView1.DataSource = dt;
        GridView1.DataBind();

        object[] rowArray = new object[3];
        rowArray[0] = 10;
        rowArray[1] = "Troubleshooting Windows® 7 Inside Out";
        rowArray[2] = "Mike Halsey";
        DataRow tempRow = dt.NewRow();
        tempRow.ItemArray = rowArray;
        dt.Rows.Add(tempRow);

        Label1.Text = "After added a new DataRow to DataTable";
            
        GridView2.DataSource = dt;
        GridView2.DataBind();
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to use DataRow ItemArray property in ado.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:DarkBlue; font-style:italic;">
            How to use DataRow ItemArray property in ado.net
        </h2>
        <hr width="500" align="left" color="CornFlowerBlue" />
        <asp:GridView 
            ID="GridView1"
            runat="server"
            BorderColor="Snow"
            ForeColor="Snow"
            Width="550"
            >
            <HeaderStyle BackColor="Firebrick" Height="35" />
            <RowStyle BackColor="ForestGreen" />
            <AlternatingRowStyle BackColor="SeaGreen" />
        </asp:GridView>
        <br />
        <asp:Label
             ID="Label1"
             runat="server"
             Font-Size="Medium"
             ForeColor="SeaGreen"
             >
        </asp:Label>
        <br /><br />
        <asp:GridView 
            ID="GridView2"
            runat="server"
            BorderColor="Snow"
            ForeColor="Snow"
            Width="550"
            ShowHeaderWhenEmpty="true"
            >
            <HeaderStyle BackColor="Firebrick" Height="35" />
            <RowStyle BackColor="ForestGreen" />
            <AlternatingRowStyle BackColor="SeaGreen" />
        </asp:GridView>
        <asp:Button 
            ID="Button1"
            runat="server"
            OnClick="Button1_Click"
            Text="Populate GridView"
            Height="45"
            Font-Bold="true"
            ForeColor="DarkBlue"
            />
    </div>
    </form>
</body>
</html>
More c# examples