DataRow Class
Represents a row of data in a DataTable.
For a list of all members of this type, see DataRow Members.
System.Object
System.Data.DataRow
[Visual Basic] <Serializable> Public Class DataRow [C#] [Serializable] public class DataRow [C++] [Serializable] public __gc class DataRow [JScript] public Serializable class DataRow
Thread Safety
This type is safe for multithreaded read operations. You must synchronize any write operations.
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 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 in relation 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 Table.
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 Deleting a Row from a Table.
Example
[Visual Basic, C#, C++] The following example creates a new DataRow by calling the NewRow method of the DataTable object.
[Visual Basic] Private Sub CreateNewDataRow() ' Use the MakeTable function below to create a new table. Dim myTable As DataTable myTable = MakeNamesTable() ' Once a table has been created, use the NewRow to create a DataRow. Dim myRow As DataRow myRow = myTable.NewRow() ' Then add the new row to the collection. myRow("fName") = "John" myRow("lName") = "Smith" myTable.Rows.Add(myRow) Dim dc As DataColumn For Each dc in myTable.Columns Console.WriteLine(dc.ColumnName) Next DataGrid1.DataSource=myTable End Sub Private Function MakeNamesTable() As DataTable ' Create a new DataTable titled 'Names.' Dim namesTable As DataTable = new DataTable("Names") ' Add three column objects to the table. Dim idColumn As DataColumn = new DataColumn() idColumn.DataType = System.Type.GetType("System.Int32") idColumn.ColumnName = "id" idColumn.AutoIncrement = True namesTable.Columns.Add(idColumn) Dim fNameColumn As DataColumn = New DataColumn() fNameColumn.DataType = System.Type.GetType("System.String") fNameColumn.ColumnName = "Fname" fNameColumn.DefaultValue = "Fname" namesTable.Columns.Add(fNameColumn) Dim lNameColumn As DataColumn = new DataColumn() lNameColumn.DataType = System.Type.GetType("System.String") lNameColumn.ColumnName = "LName" namesTable.Columns.Add(lNameColumn) ' Create an array for DataColumn objects. Dim keys(0) As DataColumn keys(0) = idColumn namesTable.PrimaryKey = keys ' Return the new DataTable. MakeNamesTable = namesTable End Function [C#] private void CreateNewDataRow(){ // Use the MakeTable function below to create a new table. DataTable myTable; myTable = MakeNamesTable(); // Once a table has been created, use the NewRow to create a DataRow. DataRow myRow; myRow = myTable.NewRow(); // Then add the new row to the collection. myRow["fName"] = "John"; myRow["lName"] = "Smith"; myTable.Rows.Add(myRow); foreach(DataColumn dc in myTable.Columns) Console.WriteLine(dc.ColumnName); dataGrid1.DataSource=myTable; } 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; } [C++] private: void CreateNewDataRow(){ // Use the MakeTable function below to create a new table. DataTable* myTable; myTable = MakeNamesTable(); // Once a table has been created, use the NewRow to create a DataRow. DataRow* myRow; myRow = myTable->NewRow(); // Then add the new row to the collection. myRow->Item[S"fName"] = S"John"; myRow->Item[S"lName"] = S"Smith"; myTable->Rows->Add(myRow); System::Collections::IEnumerator* myEnum = myTable->Columns->GetEnumerator(); while (myEnum->MoveNext()) { DataColumn* dc = __try_cast<DataColumn*>(myEnum->Current); Console::WriteLine(dc->ColumnName); } dataGrid1->DataSource=myTable; } private: DataTable* MakeNamesTable(){ // Create a new DataTable titled 'Names.' DataTable* namesTable = new DataTable(S"Names"); // Add three column objects to the table. DataColumn* idColumn = new DataColumn(); idColumn->DataType = System::Type::GetType(S"System.Int32"); idColumn->ColumnName = S"id"; idColumn->AutoIncrement = true; namesTable->Columns->Add(idColumn); DataColumn* fNameColumn = new DataColumn(); fNameColumn->DataType = System::Type::GetType(S"System.String"); fNameColumn->ColumnName = S"Fname"; fNameColumn->DefaultValue = S"Fname"; namesTable->Columns->Add(fNameColumn); DataColumn* lNameColumn = new DataColumn(); lNameColumn->DataType = System::Type::GetType(S"System.String"); lNameColumn->ColumnName = S"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; }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Namespace: System.Data
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework
Assembly: System.Data (in System.Data.dll)
See Also
DataRow Members | System.Data Namespace | AcceptChanges | Add | DataColumnCollection | DataColumn | DataRowView | DataTable | HasVersion | IsNull | Item | NewRow | DataRowCollection