title: GridView Header row Click for Sorting
body: <p>I have Created A gridview, in which there is a column which have some image buttons to perform some action. along with it, i have also created sorting event on header click.<img src="https://i.stack.imgur.com/HPw6i.png" alt="Image for gridview is enclosed in the attachment"></p>
  
  <p>When i click over "Task Title" Header, command goes to RowCommand event of Grid View where i have detected a image button and perfromed actions according to their CommandName.</p>
  
  <p>But the problem is i want to sort column and command is directed to rowCommand where it is unable to find imagebutton.</p>
  
  <p>How can i resolve this ??</p>
  
  <p>aspx code is :</p>
  
  <pre><code>&lt;asp:GridView ID="TableTask" runat="server" BackColor="White" BorderColor="#CCCCCC"
          BorderStyle="None" BorderWidth="1px" CellPadding="3" AutoGenerateColumns="False"
          ViewStateMode="Enabled" Width="300px" Style="margin-left: 50px; margin-top: 50px;
          margin-bottom: 50px;" RowStyle-HorizontalAlign="Center" OnRowCommand="TableTask_RowCommand"
          OnRowCreated="TableTask_RowCreated"&gt;
          &lt;Columns&gt;
              &lt;asp:TemplateField HeaderText="Task Titl`enter code here`e" SortExpression="Task_Title"&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:Label ID="lblTaskName" Text='&lt;%#BIND("Task_Title") %&gt;' runat="server"&gt;&lt;/asp:Label&gt;
                  &lt;/ItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
              &lt;asp:TemplateField HeaderText=" "&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:ImageButton ID="btnView" runat="server" CommandName="View" ImageUrl="~/Images/ViewIcon.png"
                          ToolTip="View" /&gt;&amp;nbsp;&amp;nbsp;
                      &lt;asp:ImageButton ID="btnEdit" runat="server" CommandName="Change" ImageUrl="~/Images/EditIcon.png"
                          ToolTip="Edit" /&gt;&amp;nbsp;&amp;nbsp;
                      &lt;asp:ImageButton ID="btnDelete" CommandArgument='&lt;%# Eval("Task_ID") %&gt;' runat="server"
                          CommandName="Remove" ImageUrl="~/Images/DeleteIcon.png" ToolTip="Delete" /&gt;
                  &lt;/ItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
          &lt;/Columns&gt;
          &lt;FooterStyle BackColor="White" ForeColor="#000066" /&gt;
          &lt;HeaderStyle BackColor="Gray" Font-Bold="True" ForeColor="White" /&gt;
          &lt;PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Center" /&gt;
          &lt;RowStyle ForeColor="#000066" /&gt;
          &lt;SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" /&gt;
          &lt;SortedAscendingCellStyle BackColor="#F1F1F1" /&gt;
          &lt;SortedAscendingHeaderStyle BackColor="#007DBB" /&gt;
          &lt;SortedDescendingCellStyle BackColor="#CAC9C9" /&gt;
          &lt;SortedDescendingHeaderStyle BackColor="#00547E" /&gt;
      &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>and Code behind RowCommand method is defined as follows:</p>
  
  <pre><code>protected void TableTask_RowCommand(object sender, GridViewCommandEventArgs e)
  {
      GridViewRow gvr = (GridViewRow)((ImageButton)e.CommandSource).NamingContainer;
      int rowIndex = gvr.RowIndex;
      GridViewRow gv = TableTask.Rows[rowIndex];
      Label task_title = (Label)gv.FindControl("lblTaskName");
  
      if (e.CommandName == "View")
      {
          // My code    
      }
  
      if (e.CommandName == "Change")
      {
          // My code
      }
  
      if (e.CommandName == "Remove")
      {
          // My code
      }
  }
  </code></pre>
  
  <p>It gives exception when we are creating GridViewRow object 'gvr'. </p>
  
  <p>Exception is : Unable to cast object of type 'System.Web.UI.WebControls.GridView' to type 'System.Web.UI.WebControls.ImageButton'.</p>
  
  <p>Hey guyz !! u r not getting me...
  I m saying that What should i do so that command should not go to RowCommand event while clicking over HeaderText.</p>