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
);
- row
- The DataRow to add.
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.
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 ( );
}
Sub updateCart ( src As Object, e As DataGridCommandEventArgs )
Dim dr As DataRow = 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 )
Dim item As String = e.Item.Cells ( 1 ).Text
Dim price As String = e.Item.Cells ( 2 ).Text
If CType ( e.CommandSource, Button ).CommandName = "AddToCart" Then
dr ( 0 ) = item
dr ( 1 ) = price
Cart.Rows.Add ( dr )
Else ' remove from Cart
CartView.RowFilter = "Item='" + item + "'"
If CartView.Count > 0 Then CartView.Delete ( 0 )
CartView.RowFilter = ""
End If
shopCart.DataBind ( )
End Sub |
|
C# |
VB |
DataRowCollection Members DataRowCollection.Add Overload List Clear DataTable DataRow NewRow Remove