System.Data Namespace DataTable Class
Finds and updates a specific row. If no matching row is found, a new row is created using the given values.
[ VB ]
Public Function LoadDataRow ( _
ByVal values ( ) As Object, _
ByVal fAcceptChanges As Boolean _
) As DataRow
[ C# ]
public DataRow LoadDataRow (
object [ ] values,
bool fAcceptChanges
);
[ C++ ]
public: DataRow* LoadDataRow (
Object* values __gc [ ],
bool fAcceptChanges
);
[ JScript ]
public function LoadDataRow (
values : Object [ ],
fAcceptChanges : Boolean
) : DataRow;
- values
- An array of values used to create the new row.
- fAcceptChanges
- true to accept changes; otherwise, false.
The new DataRow.
The LoadDataRow method takes an array of values and finds the matching value ( s ) in the primary key Column ( s ).
If a column has a default value, pass a System.Object.Empty in the array to set the default value for that column. Similarly, if a column has its AutoIncrement property set to true, pass the System.Object.Empty in the array to set the automatically generated value for the row.
If fAcceptChanges is true or not specified, the new data is added and then AcceptChanges is called to accept all changes in the DataTable; if the argument is false, newly added rows are marked as insertions.
Exceptions can also occur during either a ColumnChanging or RowChanging event. If an exception occurs, the row is not added to the table.
Use LoadDataRow in conjunction with BeginLoadData and EndLoadData.
The following example uses the LoadDataRow method to attempt to find a row. If no such row is found, the values are used to create a new row.
Private void MyAddUpdate ( DataTable myTable ) {
// create an array for the values.
object [ ] newRow = new object [ 2 ];
// set the values of the array.
newRow [ 0 ] = "Hello";
newRow [ 1 ] = "World";
newRow [ 2 ] = "two";
DataRow myRow;
myTable.BeginLoadData ( );
// add the new row to the rows collection.
myRow = myTable.LoadDataRow ( newRow, true );
myTable.EndLoadData ( );
}
Private Sub MyAddUpdate ( ByVal myTable As DataTable )
' create an array for the values.
Dim newRow ( 2 ) As Object
' set the values of the array.
newRow ( 0 ) = "Hello"
newRow ( 1 ) = "World"
newRow ( 2 ) = "two"
Dim myRow As DataRow
myTable.BeginLoadData ( )
' add the new row to the rows collection.
myRow = myTable.LoadDataRow ( newRow, True )
myTable.EndLoadData ( )
End Sub |
|
C# |
VB |
DataTable Members