Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

How can i add or remove rows from the DataTable object of DataSet?

user-image
Question added by Mohamed Shameer , OpenText ECM Consultant & ECM Administrator , Petrochemical Industrial Company
Date Posted: 2014/06/05
Noor Ahmed Syed
by Noor Ahmed Syed , IT Manager , Haji Abdullah Alireza & Co. Ltd.

‘DataTable’ has “DataRowCollection” object which has all rows in a “DataTable” object.'Add' method of the DataRowCollection is used to add a new row in DataTable.'Remove' method of the DataRowCollection is used to remove a ‘DataRow’ object from ‘DataTable’.'RemoveAt' method of the DataRowCollection is used to remove a ‘DataRow’ object from ‘DataTable’ as per the index specified in the DataTable.

Deleted user
by Deleted user

To add new row ,one can simply do this :

  // get the DataTable 

   DataTabel dt = new DataTable();

   DataRow dr = dt.NewRow();

   dr[0] = "random_data";

   .

    . .. // so on

   dt.Rows.add(dr);

 

Similary for deleting any row from Datatable :

 // dt is your DataTable

   for( int iterator i = dt.Rows.Count-1;iterator>=0;iterator--)

   {

        DataRow dr =  dt.Rows[i];

        if(dr['yourColName'] == 'yourValue' ) // for conditional deleting , else remove this condition 

           dr.Delete(); 

    }

 

More Questions Like This