Solved

Icon Conditional Format in Gridview

Posted on 2014-03-24
4
772 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
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.…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

706 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now