Solved

Icon Conditional Format in Gridview

Posted on 2014-03-24
4
808 Views
Last Modified: 2014-03-26
I have a Gridview and I have a column heading named 'Locked'. The output is either a 'Y' or 'N'. I would like to have it substituted with an icon. So when it has a 'Y' I would like an icon of a green lock icon (Which I have in the ~\Content\Images folder). Also when it is a 'N' I would like to have it substituted with a Green Lock icon. This is very similar to Excel's icon conditional formatting. Any clues?
0
Comment
Question by:Southern_Gentleman
  • 2
4 Comments
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 39952404
Use a template field, with an image inside it and bind the image source to a field in your query which is conditional on whether its locked or not.
0
 
LVL 19

Accepted Solution

by:
Manoj Patil earned 500 total points
ID: 39952859
Hey for this you need to do it by following way,

First Take an Label in your template filed. Your template filed may looks like below.
Set Visible=False for Label

<asp:TemplateField HeaderText="Status">                
                    <ItemTemplate>
                    <asp:Image ID="imgStatus" runat="server"  />
                        <asp:Label ID="lblStatus" runat="server" 
                       Text='<%#Eval("Status") %>'   Visible="false" />
                    </ItemTemplate>
                </asp:TemplateField>

Open in new window


In code behind add below code and bind this method RowDataBound to the GridView

DON'T FORGET TO CHANGE THE CELL NUMBER IN IF CONDITION. IT IS THE COLUMN NUMBER IN WHICH THE LABEL IS GOING TO BE SET.

protected void gvDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
   try
   {
         if (e.Row.RowType == DataControlRowType.DataRow)
         {
               if (e.Row.Cells[6].HasControls())
               {
                   Label lblStatus = (Label)e.Row.Cells[6].FindControl("lblStatus");
                   System.Web.UI.WebControls.Image imgbtn = ((System.Web.UI.WebControls.Image)e.Row.Cells[6].FindControl("imgStatus"));
                        if (lblStatus != null)
                        {
                            if (lblStatus.Text == "Active")
                            {
                                imgbtn.ImageUrl = "~/Setting/images/icon/color_18/hand_thumbsup.png";
                                imgbtn.ToolTip = "Active";
                            }
                            else if (lblStatus.Text == "DeActive")
                            {
                                imgbtn.ImageUrl = "~/Setting/images/icon/color_18/hand_thumbsdown.png";
                                imgbtn.ToolTip = "Deactive";
                            }
                        }
             }

     }
  catch {    }
}

Open in new window

0
 

Author Comment

by:Southern_Gentleman
ID: 39954831
So I set my Gridview field template (Locked) and then assigned the code behind but it is coming up blank for that column. Here is my code. 1st for the ASP, then the code behind. I'm also using Devexpress for my objects.  

<dx:GridViewDataTextColumn Caption="Locked" FieldName="Locked" ShowInCustomizationForm="True" VisibleIndex="7">
                                    <DataItemTemplate>
                                        <asp:Image ID="imgStatus" runat="server" Height="20px" Width="20px" />
                                        <asp:Label ID="lblStatus" runat="server" Text='<%#Eval("Locked")%>' Visible="false" />                                         
                                    </DataItemTemplate>
                                    <HeaderStyle HorizontalAlign="Center" />
                                    <CellStyle HorizontalAlign="Center" Wrap="False">
                                        </CellStyle>
                                </dx:GridViewDataTextColumn>


 Protected Sub ASPxGridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs)
        Try
            If e.Row.RowType = DataControlRowType.DataRow Then
                If e.Row.Cells(7).HasControls() Then
                    Dim lblStatus As Label = DirectCast(e.Row.Cells(7).FindControl("lblStatus"), Label)
                    Dim imgbtn As System.Web.UI.WebControls.Image = DirectCast(e.Row.Cells(7).FindControl("imgStatus"), System.Web.UI.WebControls.Image)
                    If lblStatus IsNot Nothing Then
                        If lblStatus.Text = "Y" Then
                            imgbtn.ImageUrl = "~/Images/greenlock.png"
                            imgbtn.ToolTip = "Y"
                        ElseIf lblStatus.Text = "N" Then
                            imgbtn.ImageUrl = "~/Images/redlock.png"
                            imgbtn.ToolTip = "N"
                        End If
                    End If

                End If
            End If
        Catch
        End Try

        If True Then
        End If
    End Sub

Open in new window

0
 

Author Closing Comment

by:Southern_Gentleman
ID: 39956405
I actually figured it out. Thanks again for leading me in the right direction.
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question