Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Icon Conditional Format in Gridview

Posted on 2014-03-24
4
Medium Priority
?
860 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

721 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