title: How to adjust the order of data in Dataset by Date asc? , C#
body: <p>I am assigning data to a <code>Dataset</code> manually from the data of another <code>Dataset</code>, in which two conditions are met one when there is data from another <code>Dataset</code> where the <code>date</code> field matches the rows with this data, otherwise I add them as a new row. This is perfect.</p>
  
  <p>The problem is that when you finish assigning the data to the <code>Dataset</code>, the data that shows first are the ones that are updated with the first condition.</p>
  
  <blockquote>
    <p>I need the data to be sorted by Date either <code>dd\MM\yyyy</code> or <code>yyyy/MM/dd</code> without taking into account the hours, and regardless of whether it was updated or added, but to order it by <code>Date</code> of <code>ASC</code> mode.</p>
  </blockquote>
  
  <pre><code>//foreach (DataRow dr in ds.Tables[0].Rows)
  foreach (DataRow dr in ds.Tables[0].AsEnumerable().OrderBy(x=&gt;x.Field&lt;DateTime&gt;("date").Date).ToList())
  {
      var row=dataset_manually.Tables[0].AsEnumerable().Where(x=&gt; x.Field&lt;DateTime&gt;("date").Date == ((DateTime)dr["date"]).Date).FirstOrDefault();
  
      if (row!=null)
      {
           //Update the data
           row["entryToTurn"]=(DateTime)dr["entryToTurn"];
           row["departureToTurn"]=(DateTime)dr["departureToTurn"];
           row["turn"]=dr["turn"].ToString();
      }
      else
      {
           //Add new rows
           var row2= dataset_manually.Tables[0].NewRow();
           row2["entryToTurn"] = dr["entryToTurn"];
           row2["departureToTurn"] = dr["departureToTurn"];
           row2["turn"] = dr["turn"].ToString();
           dataset_manually.Tables[0].Rows.Add(fila2);
      }
  }
  </code></pre>
  
  <blockquote>
    <p>For some reason the <code>OrderBy</code> in the <code>foreach</code> does not work. adds the data but does not order it. <code>foreach (DataRow dr in ds.Tables[0].AsEnumerable().OrderBy(x=&gt;x.Field&lt;DateTime&gt;("date").Date).ToList())</code></p>
  </blockquote>
  
  <p>I tried this but it did not work:</p>
  
  <pre><code>dataset_manually.Tables[0].DefaultView.Sort = "date asc";
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/axlFW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/axlFW.png" alt="enter image description here"></a></p>
  
  <p>And I can not do it like this:</p>
  
  <pre><code>datagridview1.Sort(datagridview1.Columns[0], ListSortDirection.Ascending);
  </code></pre>
  
  <p>Because this only shows the data ordered in the <code>DataGridView</code> but when using the <code>Dataset</code> in another side the data is not sorted and the positions of the values in the grid are not the same things in the <code>Dataset</code>.</p>
  
  <p><strong>Desired output:</strong></p>
  
  <p><a href="https://i.stack.imgur.com/klgRy.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/klgRy.png" alt="enter image description here"></a></p>
  
  <p>Note: The data is NOT assigned by the <code>index</code>, it must be assigned where the <code>date</code> field matches</p>
  
  <p><strong>Environment:</strong> Visual Studio 2010 (WindowsForms C#) &amp; .NET NetFramework 4</p>