title: Group a DataTable into separate DataTables?
body: <p>I have a DataTable which is like this - </p>
  
  <pre><code>EmpId,Country 
  1,Germany
  2,Germany
  3,UK
  4,UK
  5,UK
  6,France
  7,USA
  8,USA
  </code></pre>
  
  <p>I want to group by Country and extract all groups into separate datatables. So, we 
  will have 4 groups/data tables. How do I do this ?</p>
  
  <p>Stackoverflow won't let me paste fully working code. So here it is, thanks to 
  @Myles B. Currie </p>
  
  <p>Tested code - </p>
  
  <pre><code>public void main()
     {
          DataSet ds = (DataSet) Dts.Variables["obj_Rs"].Value;
          DataTable dt = ds.Tables[0];
  
          List&lt;DataTable&gt; allCountryTables = new List&lt;DataTable&gt;();
  
          DataView view = new DataView(dt);
          DataTable distinctValues = view.ToTable(true, "Country");
  
          //Create new DataTable for each of the distinct countries 
          //identified and add to allCountryTables list
  
          foreach (DataRow row in distinctValues.Rows)
          {
              //Remove filters on view
              view.RowFilter = String.Empty;
              //get distinct country name
              String country = row["Country"].ToString();
              //filter view for that country
              view.RowFilter = "Country = " + "'" + country + "'";
              //export filtered view to new datatable
              DataTable countryTable = view.ToTable();
              //add new datatable to allCountryTables 
              allCountryTables.Add(countryTable);
          }//for each
  
          foreach(DataTable tbl in allCountryTables){
  
              String table = getDataTable(tbl);
              MessageBox.Show(table);
  
          }//for
  }//main
  
  
      public static string getDataTable(DataTable dt){
  
          string table = "";
  
          foreach (DataRow dataRow in dt.Rows)
          {
              foreach (var item in dataRow.ItemArray)
              {
                  table = table + item.ToString() + "|";
              }//for
  
              table = table + Environment.NewLine;
  
          }//for
  
          return table;
  
      }//method
  </code></pre>