How to sort a grid view column that has an image

advertisements
<asp:TemplateField HeaderText="Status" ItemStyle-Width="15%">
                                <ItemTemplate>
                                    <asp:Image ID="Status" runat="server" />
                                </ItemTemplate>
                            </asp:TemplateField>

I have this Column in my grid view and there is no column as status in my table, I do rowdatabound to display a image based on the values in other columns,

   protected void MyGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Image img = (Image)e.Row.FindControl("Status");
            DateTime received;
            DateTime read;
            DateTime.TryParse(Convert.ToString(DataBinder.Eval(e.Row.DataItem, "TimeReceived")), out received);
            DateTime.TryParse(Convert.ToString(DataBinder.Eval(e.Row.DataItem, "TimeRead")), out read);
            if (received == DateTime.MinValue)
            {
                img.ImageUrl = "Styles/Images/red.png";
                img.ToolTip = "Message Not Received";
            }
            else (read == DateTime.MinValue)
            {
                img.ImageUrl = "Styles/Images/amber.png";
                img.ToolTip = "Message Received";
            }
                          img.Visible = true;
        }
    }

How do I sort the status column, Other column I have sort expression as column name and I am sorting it. But for this column with image how can I do the sort.


I normally already have a property on my object from the backend set that would hold the "Status", then you could sort on it and still apply your UI logic.

<asp:TemplateField HeaderText="Status" SortExpression="MessageStatus">
    <ItemTemplate>
       <asp:Image ID="Status" runat="server" />
    </ItemTemplate>
</asp:TemplateField>

public YourObject
{
   public string MessageStatus {get; set;}
   ..........
}

Put logic in the business layer when calling the query:

//call db
//fill object
   //while filling object
     DateTime received = this.TimeReceived
     DateTime read = this.TimeRead
     if (received == DateTime.MinValue)
     {
         MessageStatus  = "Message Not Received";
     }
     else (read == DateTime.MinValue)
     {
         MessageStatus  = "Message Received";
     }

RowDataBound:

protected void MyGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Image img = (Image)e.Row.FindControl("Status");

        string messsageStatus = DataBinder.Eval(e.Row.DataItem, "MessageStatus") as string;

        if (messsageStatus == "Message Not Received")
        {
            img.ImageUrl = "Styles/Images/red.png";
            img.ToolTip = messsageStatus ;
        }
        else if (messsageStatus == "Message Received")
        {
            img.ImageUrl = "Styles/Images/amber.png";
            img.ToolTip = messsageStatus ;
        }
        img.Visible = true;
    }
}