[ Team LiB ] |
25.2 Properties Reference
Gets a value indicating whether there are errors in the DataRow. ExampleThe following example shows how to use the HasErrors property to determine if there are any errors in the DataRow after the reconciliation of the modified DataTable with the data source: DataTable dt = new DataTable(); SqlDataAdapter da = new SqlDataAdapter(); // ... define the DataAdapter // fill the DataTable da.Fill(dt); // ... modify the data in the DataTable da.Update(dt); if (dt.HasErrors) { foreach(DataRow row in dt.Rows) { if (row.HasErrors) { // ... handle the errors for the row } } } NotesThe SetColumnError method can set an error on a column in the row. The GetColumnsInError methods can retrieve the columns in the row with errors. The GetColumnError can retrieve the error description for a column. The ClearErrors method can clear all errors on the row.
The indexer for the class that gets or sets the data stored in the row for the specified column. Parameters
ExampleThe following examples show how to get and set the values for a column using the different versions of the Item property: DataTable dt = new DataTable(); DataColumn col = dt.Columns.Add("MyColumn", typeof(System.String)); DataRow row = dt.NewRow(); // the next three statements have the same result row["MyColumn"] = "My value"; row[col] = "My value"; row[0] = "My value"; String rowValue=""; // the next three statements return the same result rowValue = (String) row["MyColumn", DataRowVersion.Current]; rowValue = (String) row[col, DataRowVersion.Current]; rowValue = (String) row[0, DataRowVersion.Current]; NotesIf the specified column can't be found, or the specified column index doesn't exist, an IndexOutOfRangeException is raised. If the specified column reference is null, an ArgumentNullException is raised. If the column referenced doesn't belong to the table, an ArgumentException is raised. If the data type of the column value specified doesn't match the DataType for the column, an InvalidCastException is raised. If an attempt is made to set a value on a deleted row, a DeletedRowInaccessibleException is raised. If an attempt is made to access a version of the row that doesn't exist, a VersionNotFoundException is raised.
Gets or sets the values of the row using an object array. Each item in the object array corresponds to a column in the DataTable. ExampleThe following example demonstrates how to get and set the values for columns in a row using the ItemArray property: // create a table with two columns DataTable dt = new DataTable(); DataColumn colId = new DataColumn("ProductId", typeof(System.Int32)); DataColumn colDesc = new DataColumn("Description", typeof(System.String)); dt.Columns.AddRange(new DataColumn[] {colId, colDesc}); dt.Rows.Add(new object[] {1, "Widget"}); // get the data for the row using the ItemArray property object[] row = dt.Rows[0].ItemArray; // set the ProductId to be AutoIncrement colId.AutoIncrement = true; // pass null for the AutoIncrement value dt.Rows.Add(new object[] {null, "Thing"}); // let the description be null colDesc.AllowDBNull = true; // add a row with a null description, and AutoIncrement Id dt.Rows.Add(new object[] {null, null}); NotesWhen the ItemArray property is used, an attempt is made to locate the row matching the primary key. If the row is found, it is updated with the values in the ItemArray; otherwise, a new row is created. Any columns with an array element set to null are set to the default value for a new column or the existing value for the column if the row is being updated. The value for AutoIncrement columns should be set to null in the ItemArray.
Gets or sets a value containing the error description text that applies to the entire DataRow. ExampleThe following example shows how to set the error text for a DataRow: DataTable dt = new DataTable(); DataColumn col = dt.Columns.Add("MyColumn", typeof(System.String)); DataRow row = dt.NewRow(); row.RowError = "This row has an error."; NotesThe RowError property can be set when processing modified data against the business rules. Alternatively, when the Update( ) method of the DataAdapter is called and fails and the ContinueUpdateOnError property of the DataAdapter is set to true, the HasErrors property of the row is set to true and the RowError property is set to the error message. The RowUpdated event handler can be used to determine the status of the update attempt and set the error text for the row as necessary. Use the GetColumnError( ) and SetColumnError( ) methods to get and set the error description for a particular column.
Gets the current row state of a DataRow within the DataRowCollection. This value is one of the DataRowState values described in Table 25-4.
ExampleThe value of the RowState property can't be directly set. ADO.NET sets the row state in response to actions that affect the DataRow. The AcceptChanges( ) and RejectChanges( ) methods, whether explicitly or implicitly called, reset the RowState value for the row to Unchanged as illustrated in the following code: // create a table with one column DataTable dt = new DataTable(); dt.Columns.Add("MyColumn", typeof(System.String)); // create a new row DataRow row = dt.NewRow(); // RowState = Detached // add the row to the table dt.Rows.Add(row); // RowState = Added dt.AcceptChanges(); // RowState = Unchanged // modify the row row["MyColumn"] = "MyFieldValue"; // RowState = Modified // reject the changes row.RejectChanges(); // RowState = Unchanged // delete the row row.Delete(); // RowState = Deleted row.AcceptChanges(); // row no longer exists NoteThe RowState property is used primarily by ADO.NET to track the changes that have been made to a disconnected row so that changes to the data while disconnected can be updated back to the data source
Gets the table that the row belongs to and has a schema for. ExampleThe following example demonstrates how to retrieve the DataTable the row belongs to: DataTable dt1 = new DataTable(); DataRow row = DataTable.NewRow(); // returns a reference to DataTable dt1 DataTable dt2 = row.Table; NoteA row belongs to the DataTable once it is added to the DataRowCollection. If the DataRow doesn't belong to a table, a null reference is returned. |
[ Team LiB ] |