DataRow ItemArray Property
.NET framework's DataRow class represents a row of data in a DataTable.
DataRow class exists in the System.Data namespace.
DataRow class’s ItemArray property allows 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 the ItemArray property to set a row value, the Array must have the same size and order as the DataTable's column collection. Passing null in ItemArray indicates that no value was specified.
DataRow ItemArray property throws an ArgumentException exception if the Array is larger than the table columns number. It throws InvalidCastException if a value in the Array does not match its DataType in its respective DataColumn. The property throws ConstraintException if an edit broke a constraint.
It throws ReadOnlyException if an edit tried to change the value of a read-only column. ItemArray property also throws NoNullAllowedException and DeletedRowInaccessibleException. Developers can generate exceptions in the ColumnChanging event or RowChanging event.
The following ADO.NET C# example code demonstrates to us how can we use the DataRow ItemArray property in an ASP.NET application.
DataRow class’s ItemArray property allows 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 the ItemArray property to set a row value, the Array must have the same size and order as the DataTable's column collection. Passing null in ItemArray indicates that no value was specified.
DataRow ItemArray property throws an ArgumentException exception if the Array is larger than the table columns number. It throws InvalidCastException if a value in the Array does not match its DataType in its respective DataColumn. The property throws ConstraintException if an edit broke a constraint.
It throws ReadOnlyException if an edit tried to change the value of a read-only column. ItemArray property also throws NoNullAllowedException and DeletedRowInaccessibleException. Developers can generate exceptions in the ColumnChanging event or RowChanging event.
The following ADO.NET C# example code demonstrates to us how can we use the 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>