Draft for Information Only
Content
System.Data Namespace Component DataRow Class Definition In this article Examples Remarks Constructors Properties Methods Extension Methods Applies to .NET Core .NET Framework .NET Standard Xamarin.Android Xamarin.iOS Xamarin.Mac Thread Safety See also Examples Source/Reference
System.Data Namespace Component
The System.Data namespace provides access to classes that represent the ADO.NET architecture. ADO.NET lets you build components that efficiently manage data from multiple data sources.
DataRow Class
Definition
Namespace:
System.Data
Assemblies:
System.Data.dll, netstandard.dll, System.Data.Common.dll
Represents a row of data in a DataTable .
In this article
Definition
Examples
Remarks
Constructors
Properties
Methods
Extension Methods
Applies to
Thread Safety
See also
C#
[System.Serializable]
public class DataRow
Inheritance
Object
DataRow
Attributes
SerializableAttribute
Examples
The following example creates a new DataRow by calling the NewRow method of the DataTable object.
C#
private void CreateNewDataRow()
{
// Use the MakeTable function below to create a new table.
DataTable table;
table = MakeNamesTable();
// Once a table has been created, use the
// NewRow to create a DataRow.
DataRow row;
row = table.NewRow();
// Then add the new row to the collection.
row["fName"] = "John";
row["lName"] = "Smith";
table.Rows.Add(row);
foreach(DataColumn column in table.Columns)
Console.WriteLine(column.ColumnName);
dataGrid1.DataSource=table;
}
private DataTable MakeNamesTable()
{
// Create a new DataTable titled 'Names.'
DataTable namesTable = new DataTable("Names");
// Add three column objects to the table.
DataColumn idColumn = new DataColumn();
idColumn.DataType = System.Type.GetType("System.Int32");
idColumn.ColumnName = "id";
idColumn.AutoIncrement = true;
namesTable.Columns.Add(idColumn);
DataColumn fNameColumn = new DataColumn();
fNameColumn.DataType = System.Type.GetType("System.String");
fNameColumn.ColumnName = "Fname";
fNameColumn.DefaultValue = "Fname";
namesTable.Columns.Add(fNameColumn);
DataColumn lNameColumn = new DataColumn();
lNameColumn.DataType = System.Type.GetType("System.String");
lNameColumn.ColumnName = "LName";
namesTable.Columns.Add(lNameColumn);
// Create an array for DataColumn objects.
DataColumn [] keys = new DataColumn [1];
keys[0] = idColumn;
namesTable.PrimaryKey = keys;
// Return the new DataTable.
return namesTable;
}
Remarks
The DataRow and DataColumn objects are primary components of a DataTable . Use the DataRow object and its properties and methods to retrieve and evaluate; and insert, delete, and update the values in the DataTable . The
DataRowCollection represents the actual DataRow objects in the DataTable , and the DataColumnCollection contains the DataColumn objects that describe the schema of the
DataTable . Use the overloaded Item[String, DataRowVersion] property to return or set the value of a DataColumn .
Use the HasVersion and IsNull properties to determine the status of a particular row value, and the RowState property to determine the state of the row relative to its parent DataTable .
To create a new DataRow , use the NewRow method of the DataTable object. After creating a new DataRow , use the Add method to add the new
DataRow to the DataRowCollection . Finally, call the AcceptChanges method of the DataTable object to confirm the addition. For more information about adding data to a DataTable , see
Adding Data to a DataTable .
You can delete a DataRow from the DataRowCollection by calling the Remove method of the DataRowCollection , or by calling the Delete method of the
DataRow object. The Remove method removes the row from the collection. In contrast, Delete marks the DataRow for removal. The actual removal occurs when you call AcceptChanges method. By calling
Delete , you can programmatically check which rows are marked for removal before actually deleting them. For more information, see DataRow Deletion .
Constructors
DataRow(DataRowBuilder)
Initializes a new instance of the DataRow. Constructs a row from the builder. Only for internal usage.
Properties
Methods
AcceptChanges()
Commits all the changes made to this row since the last time AcceptChanges() was called.
BeginEdit()
Starts an edit operation on a DataRow object.
CancelEdit()
Cancels the current edit on the row.
ClearErrors()
Clears the errors for the row. This includes the RowError and errors set with SetColumnError(Int32, String) .
Delete()
Deletes the DataRow .
EndEdit()
Ends the edit occurring on the row.
Equals(Object)
Determines whether the specified object is equal to the current object.
(Inherited from Object )
GetChildRows(DataRelation)
Gets the child rows of this DataRow using the specified DataRelation .
GetChildRows(DataRelation, DataRowVersion)
Gets the child rows of a DataRow using the specified DataRelation , and DataRowVersion .
GetChildRows(String)
Gets the child rows of a DataRow using the specified RelationName of a DataRelation .
GetChildRows(String, DataRowVersion)
Gets the child rows of a DataRow using the specified RelationName of a DataRelation , and DataRowVersion .
GetColumnError(DataColumn)
Gets the error description of the specified DataColumn .
GetColumnError(Int32)
Gets the error description for the column specified by index.
GetColumnError(String)
Gets the error description for a column, specified by name.
GetColumnsInError()
Gets an array of columns that have errors.
GetHashCode()
Serves as the default hash function.
(Inherited from Object )
GetParentRow(DataRelation)
Gets the parent row of a DataRow using the specified DataRelation .
GetParentRow(DataRelation, DataRowVersion)
Gets the parent row of a DataRow using the specified DataRelation , and DataRowVersion .
GetParentRow(String)
Gets the parent row of a DataRow using the specified RelationName of a DataRelation .
GetParentRow(String, DataRowVersion)
Gets the parent row of a DataRow using the specified RelationName of a DataRelation , and DataRowVersion .
GetParentRows(DataRelation)
Gets the parent rows of a DataRow using the specified DataRelation .
GetParentRows(DataRelation, DataRowVersion)
Gets the parent rows of a DataRow using the specified DataRelation , and DataRowVersion .
GetParentRows(String)
Gets the parent rows of a DataRow using the specified RelationName of a DataRelation .
GetParentRows(String, DataRowVersion)
Gets the parent rows of a DataRow using the specified RelationName of a DataRelation , and DataRowVersion .
GetType()
Gets the Type of the current instance.
(Inherited from Object )
HasVersion(DataRowVersion)
Gets a value that indicates whether a specified version exists.
IsNull(DataColumn)
Gets a value that indicates whether the specified DataColumn contains a null value.
IsNull(DataColumn, DataRowVersion)
Gets a value that indicates whether the specified DataColumn and DataRowVersion contains a null value.
IsNull(Int32)
Gets a value that indicates whether the column at the specified index contains a null value.
IsNull(String)
Gets a value that indicates whether the named column contains a null value.
MemberwiseClone()
Creates a shallow copy of the current Object .
(Inherited from Object )
RejectChanges()
Rejects all changes made to the row since AcceptChanges() was last called.
SetAdded()
Changes the RowState of a DataRow to Added.
SetColumnError(DataColumn, String)
Sets the error description for a column specified as a DataColumn .
SetColumnError(Int32, String)
Sets the error description for a column specified by index.
SetColumnError(String, String)
Sets the error description for a column specified by name.
SetModified()
Changes the RowState of a DataRow to Modified.
SetNull(DataColumn)
Sets the value of the specified DataColumn to a null value.
SetParentRow(DataRow)
Sets the parent row of a DataRow with specified new parent DataRow .
SetParentRow(DataRow, DataRelation)
Sets the parent row of a DataRow with specified new parent DataRow and DataRelation .
ToString()
Returns a string that represents the current object.
(Inherited from Object )
Extension Methods
Field<T>(DataRow, DataColumn)
Provides strongly-typed access to each of the column values in the specified row. The Field<T>(DataRow, DataColumn) method also supports nullable types.
Field<T>(DataRow, DataColumn, DataRowVersion)
Provides strongly-typed access to each of the column values in the specified row. The Field<T>(DataRow, DataColumn, DataRowVersion) method also supports nullable types.
Field<T>(DataRow, Int32)
Provides strongly-typed access to each of the column values in the specified row. The Field<T>(DataRow, Int32) method also supports nullable types.
Field<T>(DataRow, Int32, DataRowVersion)
Provides strongly-typed access to each of the column values in the specified row. The Field<T>(DataRow, Int32, DataRowVersion) method also supports nullable types.
Field<T>(DataRow, String)
Provides strongly-typed access to each of the column values in the specified row. The Field<T>(DataRow, String) method also supports nullable types.
Field<T>(DataRow, String, DataRowVersion)
Provides strongly-typed access to each of the column values in the specified row. The Field<T>(DataRow, String, DataRowVersion) method also supports nullable types.
SetField<T>(DataRow, DataColumn, T)
Sets a new value for the specified column in the DataRow . The SetField<T>(DataRow, DataColumn, T) method also supports nullable types.
SetField<T>(DataRow, Int32, T)
Sets a new value for the specified column in the DataRow the method is called on. The SetField<T>(DataRow, Int32, T) method also supports nullable types.
SetField<T>(DataRow, String, T)
Sets a new value for the specified column in the DataRow . The SetField<T>(DataRow, String, T) method also supports nullable types.
Applies to
.NET Core
3.0 Preview 8 2.2 2.1 2.0
.NET Framework
4.8 4.7.2 4.7.1 4.7 4.6.2 4.6.1 4.6 4.5.2 4.5.1 4.5 4.0 3.5 3.0 2.0 1.1
.NET Standard
2.1 Preview 2.0
Xamarin.Android
7.1
Xamarin.iOS
10.8
Xamarin.Mac
3.0
Thread Safety
This type is safe for multithreaded read operations. You must synchronize any write operations.
See also
Examples Examples of DataColumn Class
ASP.NET Code Input: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Sample Page</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<script runat="server" >
Sub Page_Load()
Dim xstr As String
Dim xconn As New System.Data.OleDb.OleDbConnection
xconn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=T:\test.mdb;User Id=admin;Password=;"
xconn.Open()
xstr = xstr + "Connection xconn to database test.mdb is opened successfully.<br />"
Dim xdata As New System.Data.DataSet
Dim xadapt As System.Data.OleDb.OleDbDataAdapter
Dim sql As String = "SELECT * FROM T1"
xadapt = New System.Data.OleDb.OleDbDataAdapter(sql, xconn)
xstr = xstr + "Dataadapter xadapt is assigned to SELECT * FROM T1 through xconn successfully.<br />"
xadapt.Fill(xdata,"T1")
xstr = xstr + "Dataset xdata is filled by xadapt.fill successfully.<br />"
xstr = xstr + "<br />"
Dim xrow As System.Data.DataRow
xstr = xstr + "Declare a table row, xrow<br />"
xrow = xdata.tables(0).rows(0)
xstr = xstr + "Assign xdata.tables(0).Rows(0) to table row, xrow <br />"
xstr = xstr + "->xrow.Item(0): " + xrow.Item(0).ToString + "<br />"
xstr = xstr + "->xrow.Item(f2): " + xrow.Item("f2").ToString + "<br />"
xstr = xstr + "<br />"
xrow.BeginEdit()
xstr = xstr + "xrow.BeginEdit<br />"
xrow.Item("f2")="22"
xstr = xstr + "Set xrow.Item(f2)=22<br />"
xrow.CancelEdit()
xstr = xstr + "xrow.CancelEdit<br />"
xstr = xstr + "->xrow.Item(f2): " + xrow.Item("f2").ToString + "<br />"
xrow.Item("f2")="22"
xstr = xstr + "Set xrow.Item(f2)=22<br />"
xrow.EndEdit()
xrow.AcceptChanges()
xstr = xstr + "xrow.EndEdit and xrow.AcceptChanges<br />"
xrow.CancelEdit()
xstr = xstr + "xrow.CancelEdit<br />"
xstr = xstr + "->xrow.Item(f2): " + xrow.Item("f2").ToString + "<br />"
xstr = xstr + "<br />"
xstr = xstr + "<br />"
xadapt.Dispose()
xstr = xstr + "Dataadapter xadapt is disposed successfully.<br />"
xconn.Close()
xstr = xstr + "Connection xconn is closed successfully.<br />"
xstr = xstr + "Dataset xdata.tables(T1).rows(0).item(2):" + xdata.Tables("T1").Rows(0).Item("f2") + "<br />"
xdata.Dispose()
xstr = xstr + "Dataset xdata is disposed successfully.<br />"
lbl01.Text = xstr
End Sub
</script>
</head>
<body>
<%Response.Write("<p>Results on "& Request.ServerVariables("SERVER_SOFTWARE") & " .net: " & System.Environment.Version.ToString & " " & ScriptEngine & " Version " & ScriptEngineMajorVersion & "." & ScriptEngineMinorVersion & "</p>")%>
<% Response.Write ("<h1>This is a Sample Page of DataColumn Class</h1>") %>
<p>
<%-- Set on Page_Load --%>
<asp:Label id="lbl01" runat="server" />
</p>
</body>
</html>
HTML Web Page Embedded Output:
Source/Reference
©sideway
ID: 201100009 Last Updated: 11/9/2020 Revision: 0 Ref:
References
Active Server Pages, , http://msdn.microsoft.com/en-us/library/aa286483.aspx
ASP Overview, , http://msdn.microsoft.com/en-us/library/ms524929%28v=vs.90%29.aspx
ASP Best Practices, , http://technet.microsoft.com/en-us/library/cc939157.aspx
ASP Built-in Objects, , http://msdn.microsoft.com/en-us/library/ie/ms524716(v=vs.90).aspx
Response Object, , http://msdn.microsoft.com/en-us/library/ms525405(v=vs.90).aspx
Request Object, , http://msdn.microsoft.com/en-us/library/ms524948(v=vs.90).aspx
Server Object (IIS), , http://msdn.microsoft.com/en-us/library/ms525541(v=vs.90).aspx
Application Object (IIS), , http://msdn.microsoft.com/en-us/library/ms525360(v=vs.90).aspx
Session Object (IIS), , http://msdn.microsoft.com/en-us/library/ms524319(8v=vs.90).aspx
ASPError Object, , http://msdn.microsoft.com/en-us/library/ms524942(v=vs.90).aspx
ObjectContext Object (IIS), , http://msdn.microsoft.com/en-us/library/ms525667(v=vs.90).aspx
Debugging Global.asa Files, , http://msdn.microsoft.com/en-us/library/aa291249(v=vs.71).aspx
How to: Debug Global.asa files, , http://msdn.microsoft.com/en-us/library/ms241868(v=vs.80).aspx
Calling COM Components from ASP Pages, , http://msdn.microsoft.com/en-us/library/ms524620(v=VS.90).aspx
IIS ASP Scripting Reference, , http://msdn.microsoft.com/en-us/library/ms524664(v=vs.90).aspx
ASP Keywords, , http://msdn.microsoft.com/en-us/library/ms524672(v=vs.90).aspx
Creating Simple ASP Pages, , http://msdn.microsoft.com/en-us/library/ms524741(v=vs.90).aspx
Including Files in ASP Applications, , http://msdn.microsoft.com/en-us/library/ms524876(v=vs.90).aspx
ASP Overview, , http://msdn.microsoft.com/en-us/library/ms524929(v=vs.90).aspx
FileSystemObject Object, , http://msdn.microsoft.com/en-us/library/z9ty6h50(v=vs.84).aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/ms675944(v=vs.85).aspx, , ADO Object Model
ADO Fundamentals, , http://msdn.microsoft.com/en-us/library/windows/desktop/ms680928(v=vs.85).aspx