title: how to sort in gridview using template fields
body: <p>The sorting works fine when u fill the gridview using SQL datasource in the aspx page...</p>
  
  <p>but now i am using template field and the columns are filled separately in the codebehind and the sorting is not working...</p>
  
  <p>my code is</p>
  
  <pre><code>&lt;asp:GridView ID="GridView1" runat="server" AllowSorting="True" 
                            AutoGenerateColumns="False" 
                             ondatabound="GridView1_DataBound" 
                          onrowdatabound="GridView1_RowDataBound"&gt;
  
  
                 &lt;Columns&gt;
                 &lt;asp:TemplateField HeaderText="File Name" ItemStyle-Width="40%"  &gt;
                                  &lt;EditItemTemplate&gt;
                                      &lt;asp:TextBox ID="TextBox1" runat="server"&gt;&lt;/asp:TextBox&gt;
                                  &lt;/EditItemTemplate&gt;
                                  &lt;ItemTemplate&gt;
                                      &lt;asp:Label ID="Label1" runat="server"&gt;&lt;/asp:Label&gt;
                                  &lt;/ItemTemplate&gt;
                                  &lt;HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" /&gt;
                              &lt;/asp:TemplateField&gt;
  
                              &lt;asp:TemplateField HeaderText="Failure Count" ItemStyle-Width="10%" &gt;
                                  &lt;EditItemTemplate&gt;
                                      &lt;asp:TextBox ID="TextBox3" runat="server"&gt;&lt;/asp:TextBox&gt;
                                  &lt;/EditItemTemplate&gt;
                                  &lt;ItemTemplate&gt;
                                      &lt;asp:Label ID="Label3" runat="server"&gt;&lt;/asp:Label&gt;
                                  &lt;/ItemTemplate&gt;
                                  &lt;HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" /&gt;
                                  &lt;ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"  /&gt;
                              &lt;/asp:TemplateField&gt;
                 &lt;/Columns&gt;&lt;/GridView&gt;
  </code></pre>
  
  <p>and my codebehind is:</p>
  
  <pre><code>    DataTable dt = new DataTable();
          SqlConnection connection = new SqlConnection();
          connection.ConnectionString = ConfigurationManager.ConnectionStrings["SumooHAgentDBConnectionString"].ConnectionString;
          connection.Open();
          SqlCommand sqlCmd = new SqlCommand("SELECT  FileName,FailureCount from Files where MachineID=@strID , connection);
          SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
          sqlCmd.Parameters.AddWithValue("strID", strID);
  
          sqlDa.Fill(dt);
  
          if (dt.Rows.Count &gt; 0)
          {
              for (int i = 0; i &lt; dt.Rows.Count; i++)
              {
                  string nameoffiles = dt.Rows[i]["FileName"].ToString();
                  buFailureCode.Add(code);
                  string count = dt.Rows[i]["BuFailureCount"].ToString();
                  buFailureCount.Add(count);
              }
  
          }
          connection.Close();
      }
  
  
   protected void GridView1_DataBound(object sender, EventArgs e)
      {
          if (namesOfFiles.Count != 0)
          {
              for (int i = 0; i &lt; namesOfFiles.Count; i++)
              {
                  GridViewRow myRow = GridView1.Rows[i];
                  Label Label1 = (Label)myRow.FindControl("Label1");
  
                  Label Label3 = (Label)myRow.FindControl("Label3");
                  Label1.Text = namesOfFiles[i].ToString();
  
                  Label3.Text = buFailureCount[i].ToString();
  
              }}}
  </code></pre>