Topic: ForEach - Using DataRow in Dataset
Share/Save/Bookmark
C# - FOREACH ON DATASET
 
NOTE: 1. Look at DB - Create a connection in code with update on populating the dataset
 
// if you are going to be inserting rows into a dataset, clear it first
// if you are just updating, then you don't need to clear the dataset   (Example below)
ds_MyDataSet.Clear();
 
THE BELOW SITUATION, USES ONE DATASET (MyDataset) TO INSERT DATA INTO ANOTHER DATSET (MyOtherDataSet)
 
// Create a SQL Data Row to use to add to the dataset
DataRow newDataGridRow;
 
foreach(DataRow oldRow in ds_MyDataSet.Tables["MyTable"].Rows)
      {
            // Create a guid to identify player
            Guid myGUID;
            myGUID = Guid.NewGuid();
                       
            newDataGridRow = MyDataSet.Tables["MyDataSet"].NewRow();
            // Assign values
            newDataGridRow["MyGUID"] = myGUID;
            newDataGridRow["FirstName"] = oldRow["firstname"];
            newDataGridRow["MiddleName"] = oldRow["middlename"];
            newDataGridRow["LastName"] = oldRow["lastname"];
            // UPDATE THE DATASET YOU ARE POPULATING
            MyDataset.Tables["MyDataset "].Rows.Add(newDataGridRow);
      }
        // Update newDataGridRow   
da_DataAdapter.Update(ds_MyDataSet, " ds_MyDataSet ");
      grd_MyGrid.Refresh();
     
      da_DataAdapter.Update(ds_MyOtherDataSet, "MyOtherDataSetName");
      grd_MyGrid.Refresh();