DataTable in C#

Sometimes we have a need to store values temporarily. Every time of process we can't store the values to SQL or MS Access database. So to access temporary values we using the data table in c#. The data table gives a convenient way to store the data in memory.

Creation of data table:
The syntax of the Datatable creation is
Datatable datatablename =new Datatable ();
  1. Datatable dt =new Datatable (); 
Here dt is a Datatable name and new represents creating a new Datatable with a null value.
Add column:
To add a new column in the Data table we using the below syntax
  1. dt.Columns.Add ("Columname");  
  1. dt.Columns.Add ("Name"); 
Here we created a "Name" column in Datatable dt.
Add Row and item:
Syntax of add row,
  1. datatablename.Rows.Add();  
  2. DataRow datarowname;  
  3. datarowname =datatablename.NewRow();  
  4. datarowname["columnname"] = object;  
  5. datatablename.Rows.Add(datarowname); 
  1.  dt.Rows.Add();  
  2. DataRow dr;  
  3. dr = dt.NewRow();  
  4. dr["Name"] = "aaa";  
  5. dt.Rows.Add(dr); 
Delete Row:
Syntax for deletion:
  1. datatablename.Rows [Rowvalue].Delete ();  
  2. datatablename.RemoveAt (Rowvalue); 
  1. dt.Rows [0].Delete (); 
  1. dt.Rows.RemoveAt (0); 

Similar Articles