14.15 Modifying Rows in a DataTable

 <  Day Day Up  >  

14.15 Modifying Rows in a DataTable

You want to change column values in an existing row within a DataTable .


Technique

Once you add a row to a DataTable , you can use the Find method defined in the Rows collection of the DataTable to find a row by passing a value for the primary key. You can optionally use the indexer of the Rows collection, passing a zero-based index for the desired row to modify. Both of these techniques return a DataRow object, and any edits made to that DataRow automatically cause the updates to occur within the DataTable , as shown in the following code, which is continued from the previous section:

 
 case( 'M' ): {     Console.Write( "Enter an item ID: " );     int id = Int32.Parse( Console.ReadLine() );     DataRow modRow = table.Rows.Find( id );     Console.Write( "Enter item name: " );     modRow["Item"] = Console.ReadLine();     Console.Write( "Enter item count: " );     modRow["Count"] = Int32.Parse( Console.ReadLine() );     break; } case( 'D' ): {     Console.Write( "Enter an item ID: " );     int id = Int32.Parse( Console.ReadLine() );     DataRow delRow = table.Rows.Find( id );     table.Rows.Remove( delRow );     break; } 

If you are allowing users to enter data for a specific DataRow , you can create a custom event handler that cancels the change if the data they enter is not valid for a specific column. You add an event handler for the ColumnChanged event fired from the DataTable class. Before the value for a specific column is changed, call the BeginEdit method, and after the value is changed, call the EndEdit method. Doing so calls the ColumnChanged event handler, allowing you to ensure the entered value is valid. If it isn't, call CancelEdit on the DataRow object so the row isn't updated, as shown in the following code:

 
 public void ModifyRow() {     if( handlerAdded == false )         table.ColumnChanged+=new DataColumnChangeEventHandler(OnColumnChanged);     Console.Write( "Enter an item ID: " );     int id = Int32.Parse( Console.ReadLine() );     DataRow modRow = table.Rows.Find( id );     modRow.BeginEdit();     Console.Write( "Enter item name: " );     modRow["Item"] = Console.ReadLine();     Console.Write( "Enter item count: " );     modRow["Count"] = Int32.Parse( Console.ReadLine() );     modRow.EndEdit(); } private void OnColumnChanged(Object sender, DataColumnChangeEventArgs args) {   if (args.Column.ColumnName == "Count")     if (Convert.ToInt32(args.ProposedValue) < 0)     {       Console.WriteLine("Count cannot be less than 0");       args.Row.CancelEdit();     } } 

Comments

The example for this recipe uses an event handler to check the proposed value for a row that is being edited. The DataTable class actually contains several other events that might prove useful when manipulating data within the table. These events include the RowChanging and RowChanged events, which are called when the BeginEdit and EndEdit methods of a DataRow are called. Both of these methods use a DataRowChangeEventArgs object, which contains the current action being done to the data row accessed through the Action property. This property tells you whether the row is being added, changed, committed, deleted, or rolled back in the case of a change cancel. The other possible events include RowDeleting and RowDeleted as well as ColumnChanging and ColumnChanged .

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net