[ Team LiB ] |
28.3 Methods Reference
Inserts a new row into the underlying DataTable and returns a DataRowView object that represents the new row. The DataView.AddNew( ) method immediately adds the new row to the DataTable and implicitly calls BeginEdit( ) on the row. You must set all required values (values for columns that don't allow nulls and don't have default values) and then call DataRowView.EndEdit( ) to commit changes. ExampleHere's an example that creates a new row for the Categories table using the DataView.AddNew( ) method: DataRowView row = view.AddNew(); // Set all required values. row["CategoryName"] = "Fruit"; row["Description"] = "Pears, kiwis, and oranges"; // Commit the new row. view.EndEdit(); NoteIt's possible that the new DataRowView won't be visible in the DataView you used to create it, depending on the value of the DataView.RowFilter and DataView.RowStateFilter properties.
Removes a row at the specified index. This affects both the DataView and the underlying DataTable. As with the DataRow.Delete( ) method, the DataView.Delete( ) method marks a row for deletion by setting the DataRow.RowState property. The change doesn't become final until you call the DataTable.AcceptChanges( ) method (either directly or implicitly through the DataAdapter.Update( ) method). ExampleThis code snippet deletes all order records in which the UnitPrice field is equal to 0. DataView view = new DataView(ds.Tables["Orders"]); view.RowFilter = "UnitPrice = 0"; // Delete these rows. foreach (DataRowView row in view) { row.Delete(); }
Returns the index of a single matching row, using the current DataView sort order. For example, if you have a sort defined on the ContactName column of the Customers table, you can use the Find( ) method to search for a row with a specific ContactName. If no match is found, the Find( ) method returns -1. If there are multiple matches, only the first is returned. The Find( ) method requires exact matches. You can't perform a partial match (for example, by supplying just a first name for the ContactName) or use a wildcard. Parameters
ExampleThe following code snippet searches for an exact match of the ContactName field: DataView view = new DataView(ds.Tables["Customers"]); view.Sort = "ContactName"; int rowIndex = view.Find("Roland Mendel"); if (rowIndex == -1) { Console.WriteLine("No match found."); } else { Console.WriteLine(view[rowIndex]["CustomerID"].ToString() + " is a match."); } NotesTo use the Find( ) method, you must have defined a sort order for a DataView, either by setting the RowFilter property or the ApplyDefaultSort property. If it's possible that your search will match more than one row, use the FindRows( ) method instead of the Find( ) method. The case-sensitivity of search values for the Find( ) method is determined by the CaseSensitive property of the underlying DataTable.
Returns an array with every DataRowView object that matches a specified search expression in a given DataView. If no match is found, FindRows( ) returns an empty array. The FindRows( ) method requires exact matches. You can't perform a partial match (for example, by supplying just a first name for the ContactName). Parameters
ExampleThe following code snippet searches for an exact match of the Country field and displays the information for all matching rows: DataView view = new DataView(ds.Tables["Customers"]); view.Sort = "Country"; DataRowView[] rows = view.FindRows("Germany"); if (rows.Length == 0) { Console.WriteLine("No match found."); } else { foreach (DataRowView row in rows) { Console.WriteLine(row["CustomerID"].ToString() + " is a match."); } } NotesTo use the FindRows( ) method, you must define a sort order for a DataView, either by setting the RowFilter property or the ApplyDefaultSort property. If you want to search a unique column, you can use the Find( ) method instead of the FindRows( ) method for slightly easier coding. The case-sensitivity of search values for the FindRows( ) method is determined by the CaseSensitive property of the underlying DataTable. |
[ Team LiB ] |