Solved

Icon Conditional Format in Gridview

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

615 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