14.14 Inserting New Rows into a DataTable

 <  Day Day Up  >  

14.14 Inserting New Rows into a DataTable

You want to add data to a DataTable by inserting new rows.


Technique

A DataTable contains a collection of DataColumn objects representing each column as well as a Rows property, which you use to access a collection of DataRow objects. Creating a new row differs from creating a new column: You create the DataRow by calling the NewRow method defined in the DataTable class rather than create an instance and add it to a collection, which is the procedure for columns . After calling NewRow from a DataTable object, you can set individual column values by using the DataRow indexer. Each column name within the row is specified as a string parameter to the DataRow indexer, although you can optionally use an integer specifying the zero-based column index. Listing 14.5 demonstrates adding a new row to the DataTable and setting its initial values. Note that calling NewRow does not add the row to the DataTable . Once the column values are created, add it to the DataTable by calling the Add method defined in the Rows collection of the DataTable .

Listing 14.5 Adding New DataRow s
 using System; using System.Data; namespace _14_DataRows {     class Class1     {         [STAThread]         static void Main(string[] args)         {             char input;             // create inventory table             DataTable table = new DataTable("Inventory");             // add primary key column             DataColumn col = new DataColumn( "ID", typeof(int) );             col.AutoIncrement = true;             col.Unique = true;             table.Columns.Add( col );             table.PrimaryKey = new DataColumn[]{col};             // create item and count columns             table.Columns.Add( new DataColumn("Item", typeof(string) ));             table.Columns.Add( new DataColumn("Count", typeof(int) ));             do             {                 Console.WriteLine();                 Console.WriteLine( "A)dd new record" );                 Console.WriteLine( "M)odify record" );                 Console.WriteLine( "D)elete record" );                 Console.WriteLine( "V)iew records" );                 Console.WriteLine( "Q)uit" );                 Console.Write( "Enter command: " );                 input = Char.ToUpper(Console.ReadLine()[0]);                 switch( input )                 {                     case( 'A' ):                     {                         DataRow row = table.NewRow();                         Console.Write( "Enter item name: " );                         row["Item"] = Console.ReadLine();                         Console.Write( "Enter item count: " );                         row["Count"] = Int32.Parse( Console.ReadLine() );                         table.Rows.Add( row );                         break;                     }                     case( 'M' ):                     {                         break;                     }                     case( 'D' ):                     {                          break;                     }                     case( 'V' ):                     {                         DataRow[] currRows = table.Select(null,                             null, DataViewRowState.CurrentRows);                         if (currRows.Length < 1 )                             Console.WriteLine("No Current Rows Found");                         else                         {                             foreach (DataColumn myCol in table.Columns)                                 Console.Write("\t{0}", myCol.ColumnName);                             Console.WriteLine("\tRowState");                             foreach (DataRow myRow in currRows)                             {                                 foreach (DataColumn myCol in table.Columns)                                     Console.Write("\t{0}", myRow[myCol]);                                 Console.WriteLine("\t" + myRow.RowState);                             }                         }                         break;                     }                     case( 'Q' ):                     {                         break;                     }                     default:                     {                         Console.WriteLine( "Invalid command" );                         break;                     }                 }             } while( input != 'Q' );         }     } } 

Comments

In the sample code for this recipe, viewing the DataTable also outputs the RowState value for each row. This RowState value specifies whether the row has been added, modified, unchanged, or deleted. In this application, all rows have a RowState of Added . To change this value from Added to Unchanged , you must call the AcceptChanges method from the DataTable object. You can, however, discard any newly added rows to a DataTable by calling the RejectChanges method. When working in a disconnected state, it is common to perform batch updates to a data source using only the rows that have changed within a table. In other words, by calling the GetChanges method of the DataTable , you receive a subset of values within another DataTable corresponding to the rows that have a RowState value other than Unchanged . Once this batch update occurs, you call the AcceptChanges or RejectChanges method so that the same rows are not repeated in the next batch update.

 <  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