asp.net.ph

DataRowCollection.Add Method ( DataRow )

System.Data Namespace   DataRowCollection Class


Adds the specified DataRow object to the collection.

[ VB ]
Overloads Public Sub Add ( _
   ByVal row As DataRow _
)

[ C# ]
public void Add (
   DataRow row
);

[ C++ ]
public: void Add (
   DataRow* row
);

[ JScript ]
public function Add (
   row : DataRow
);

Parameters

row
The DataRow to add.

Exceptions


Exception Type Condition
ArgumentNullException Occurs when the row is null.
ArgumentException Occurs when the row either belongs to another table or already belongs to this table.
ConstraintException Occurs when the addition invalidates a constraint.
NoNullAllowedException Occurs when the addition tries to put a null in a DataColumn where AllowDBNull is false

Remarks

To create a new DataRow, you must use the DataTable class's NewRow method. When you use the NewRow method, a new DataRow object is returned using the schema of the parent DataTable. After you create the DataRow object and set the values for each of its columns, use the Add method to add the object to the collection.

If the user generates an exception in the RowChanging event, the row is not added to the table.

Example

The following example uses the Add method to add a new DataRow to a DataTable used to store items in a shopping cart.

void updateCart ( Object src, DataGridCommandEventArgs e ) {
   DataRow dr = Cart.NewRow ( );

   // e.Item is the row of the table where the command is raised. 
   // for bound columns, values are stored in the Text property of Cells [ colIndex ]
   string item = e.Item.Cells [ 1 ].Text;
   string price = e.Item.Cells [ 2 ].Text;

   if ( ( ( Button ) e.CommandSource ).CommandName == "AddToCart" ) {
      dr [ 0 ] = item; dr [ 1 ] = price;
      Cart.Rows.Add ( dr );
   }
   else {  // remove from Cart
      CartView.RowFilter = "Item='" + item + "'";
      if ( CartView.Count > 0 ) CartView.Delete ( 0 );
      CartView.RowFilter = "";
   }
   shopCart.DataBind ( );
}
  C# VB
Adding Rows to a DataTable
Run Sample | View Source
See Also

DataRowCollection Members   DataRowCollection.Add Overload List   Clear   DataTable   DataRow   NewRow   Remove Skip Navigation Links




Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph