Finds and updates a specific row. If no matching row is found, a new row is created using the given values.
[Visual Basic] Public Function AddUpdate( _ ByVal values() As Object _ ) As DataRow [C#] public DataRow AddUpdate( object[] values ); [C++] public: DataRow* AddUpdate( Object* values[] ); [JScript] public function AddUpdate( values : Object[] ) : DataRow;
The new DataRow.
Exception Type | Condition |
---|---|
ArgumentException | The array is larger than the number of columns in the table. |
InvalidCastException | A value doesn't match its respective column type. |
ConstraintException | Adding the row invalidates a constraint. |
NoNullAllowedException | Attempting to put a null in a column where AllowNull is false. |
The AddUpdate 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.
Exceptions can also occur during either a ColumnChanging or RowChanging event. If an exception occurs, the row is not added to the table.
The following example creates a new row
[Visual Basic]
Private Sub MyAddUpdate() Dim t As DataTable Dim rc As RowsCollection ' Create an array for the values. Dim newRow(2) As Object ' Get the DataTable of a DataGrid control. Set t = DataGrid1.DataGridTable.DataTable ' Get the table's RowsCollection. Set rc = t.Rows ' Set the values of the array. newRow(0) = "Hello" newRow(1) = "World" newRow(2) = "Hello" Dim myRow As DataRow ' Add the new row to the rows collection. Set myRow = rc.AddUpdate(newRow) ' To test, print out the values of each column in the row. Console.WriteLine(myRow(0), myRow(1), myRow(2)) End Sub
RowsCollection Class | RowsCollection Members | System.Data Namespace