title: Sorting a Gridview using Datatable
body: <p>I have a gridview which I want to sort when any of its column header is clicked. There is a DataTable that is built on runtime and assigned to the gridview to populate data. Here is the DataTable and Gridview:</p>
  
  <pre><code>DataTable dtMedication = new DataTable();
  dtClientMedications.Columns.Add("Id");
  dtClientMedications.Columns.Add("BrandName");
  dtClientMedications.Columns.Add("GenericName");
  dtClientMedications.Columns.Add("Dosage");
  dtClientMedications.Columns.Add("Physician");
  dtClientMedications.Columns.Add("DatePrescribed");
  dtClientMedications.Columns.Add("Status");
  dtClientMedications.Columns.Add("ClientMedicationDataId");
  
  &lt;asp:GridView ID="gdvMainList" runat="server" AutoGenerateColumns="False"
                              SkinID="PagedGridView" onrowcommand="gdvMainList_RowCommand" 
                              DataKeyNames="Id" onsorting="gdvMainList_Sorting"&gt;
                              &lt;PagerStyle CssClass="gridpager" HorizontalAlign="Right" /&gt;
                              &lt;Columns&gt;
                                  &lt;ucc:CommandFieldControl HeaderText="Actions" ShowDeleteButton="true" ButtonType="Image"
                                      DeleteImageUrl="~/App_Themes/Default/images/delete.png" ShowEditButton="true"
                                      EditImageUrl="~/App_Themes/Default/images/edit.png" DeleteConfirmationText="Are you sure you want to delete?"&gt;
                                      &lt;ItemStyle HorizontalAlign="Center" Width="60px" /&gt;
                                  &lt;/ucc:CommandFieldControl&gt;
                                  &lt;asp:BoundField DataField="BrandName" HeaderText="Brand Name" /&gt;
                                  &lt;asp:BoundField DataField="GenericName" HeaderText="Generic Name" /&gt;
                                  &lt;asp:BoundField DataField="Dosage" HeaderText="Dosage" /&gt;
                                  &lt;asp:BoundField DataField="Physician" HeaderText="Physician" /&gt;
                                  &lt;asp:BoundField DataField="DatePrescribed" HeaderText="Date Prescribed" /&gt;
                                  &lt;asp:BoundField DataField="Status" HeaderText="Status" /&gt;
                                  &lt;asp:TemplateField HeaderText=""&gt;
                                      &lt;ItemStyle CssClass="HiddenCol" Width="0px" /&gt;
                                      &lt;HeaderStyle CssClass="HiddenCol" /&gt;
                                      &lt;ItemTemplate&gt;
                                          &lt;asp:HiddenField ID="hdfClientMedicationDataId" runat="server" Value='&lt;%#Bind("ClientMedicationDataId") %&gt;' /&gt;
                                      &lt;/ItemTemplate&gt;
                                  &lt;/asp:TemplateField&gt;
                              &lt;/Columns&gt;
                              &lt;EmptyDataTemplate&gt;
                                  &lt;div class="divEmptyGrid"&gt;
                                      --- No Medication Exists ---
                                  &lt;/div&gt;
                              &lt;/EmptyDataTemplate&gt;
                          &lt;/asp:GridView&gt;
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/GIpFX.png" alt="enter image description here"></p>