0
votes

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.Image for gridview is enclosed in the attachment

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.

But the problem is i want to sort column and command is directed to rowCommand where it is unable to find imagebutton.

How can i resolve this ??

aspx code is :

<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">
        <Columns>
            <asp:TemplateField HeaderText="Task Titl`enter code here`e" SortExpression="Task_Title">
                <ItemTemplate>
                    <asp:Label ID="lblTaskName" Text='<%#BIND("Task_Title") %>' runat="server"></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText=" ">
                <ItemTemplate>
                    <asp:ImageButton ID="btnView" runat="server" CommandName="View" ImageUrl="~/Images/ViewIcon.png"
                        ToolTip="View" />&nbsp;&nbsp;
                    <asp:ImageButton ID="btnEdit" runat="server" CommandName="Change" ImageUrl="~/Images/EditIcon.png"
                        ToolTip="Edit" />&nbsp;&nbsp;
                    <asp:ImageButton ID="btnDelete" CommandArgument='<%# Eval("Task_ID") %>' runat="server"
                        CommandName="Remove" ImageUrl="~/Images/DeleteIcon.png" ToolTip="Delete" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <FooterStyle BackColor="White" ForeColor="#000066" />
        <HeaderStyle BackColor="Gray" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Center" />
        <RowStyle ForeColor="#000066" />
        <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#F1F1F1" />
        <SortedAscendingHeaderStyle BackColor="#007DBB" />
        <SortedDescendingCellStyle BackColor="#CAC9C9" />
        <SortedDescendingHeaderStyle BackColor="#00547E" />
    </asp:GridView>

and Code behind RowCommand method is defined as follows:

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
    }
}

It gives exception when we are creating GridViewRow object 'gvr'.

Exception is : Unable to cast object of type 'System.Web.UI.WebControls.GridView' to type 'System.Web.UI.WebControls.ImageButton'.

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.

2

2 Answers

1
votes

Handle Gridview's Sort Event,Set Allow Sorting Property To True,and Add following code Into .cs file:

    public SortDirection dir
    {
        get {
            if (ViewState["dirstate"] == null)
            {
                ViewState["dirstate"] = SortDirection.Ascending;
            }
           return (SortDirection)ViewState["dirstate"];
        }
        set {
            ViewState["dirstate"] = value;
        }
    }

    protected void grdemp_Sorting(object sender, GridViewSortEventArgs e)
    {
        BindGrid();
        string Sortdir = string.Empty;
        if (dir == SortDirection.Ascending)
        {
            dir = SortDirection.Descending;
            Sortdir = "DESC";
        }
        else
        {
            dir = SortDirection.Ascending;
            Sortdir = "ASC";
        }

        DataView dv = new DataView(dt);
        dv.Sort = e.SortExpression + " " + Sortdir;
        grdemp.DataSource = dv;
        grdemp.DataBind();


    }
-1
votes

In RowCommand Event,You Can Try Following Code:

  if(e.CommandName=="View")
  {
      int id=int.parse(e.CommandArgument.ToString());

  }

same code write to another two imagebutton

Thanks......