Connecting Tech Pros Worldwide Help | Site Map

gridview - display icon conditionally

  #1  
Old March 17th, 2009, 01:15 AM
Familiar Sight
 
Join Date: Feb 2007
Posts: 142
I have a gridview "gridview2" and here is the datasource
Expand|Select|Wrap|Line Numbers
  1. SELECT     e.emp_id, e.emp_pin, e.last_name, e.first_name, e.hiring_date, e.active, 
  2.                       e.email_address, e.hourly_employee, e.uid, a.administrator_id aid, 
  3.                       s.supervisor_id sid
  4. FROM         EMPLOYEES e LEFT OUTER JOIN
  5.                       SUPERVISORS s ON e.emp_id = s.emp_id LEFT OUTER JOIN
  6.                       ADMINISTRATORS a ON e.emp_id = a.emp_id
  7. ORDER BY e.first_name
  8.  
I am not showing the aid and sid on the gridview2.

I would like to diplay img_admin and img_super next to a record if it has something in it.

I tried to follow this link http://aspnet101.com/aspnet101/aspnet/codesample.aspx?code=gvconditionalImages code=gvconditionalImages noticed that my situation is little different. I got stuck when I started to write the class.

Last edited by tlhintoq; March 17th, 2009 at 02:35 AM. Reason: [CODE] ... [/CODE] tags added
  #2  
Old March 17th, 2009, 02:38 AM
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,690
Provided Answers: 3

re: gridview - display icon conditionally


Lots of statements... no question.

Posting guidelines... How to ask a question.
  #3  
Old March 17th, 2009, 02:44 AM
Familiar Sight
 
Join Date: Feb 2007
Posts: 142

re: gridview - display icon conditionally


Are you sure??

Read it again...

I am not showing (displaying) the aid and sid on the gridview2.

I would like to diplay img_admin and img_super next to a record if it has either sid or aid. aid will take priority over sid icon.

Is it clear now?
  #4  
Old March 17th, 2009, 03:17 AM
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,690
Provided Answers: 3

re: gridview - display icon conditionally


Yep. I'm pretty sure. There is no question in that post... Yep. Not one sentence that ends in a question mark.

You have stated what you aren't getting:
Quote:
I am not showing (displaying) the aid and sid on the gridview2.
Quote:
Expand|Select|Wrap|Line Numbers
  1. a.administrator_id aid, 
  2.                       s.supervisor_id sid
Those of us on the outside have no idea what these variables or values are.
Is a.administor_id an int? ... a string? ... A jpg ?

And you've stated what your desires are:
Quote:
I would like to diplay img_admin and img_super next to a record if it has either sid or aid. aid will take priority over sid icon.
The very fact that you say "If it has either a sid or aid" tells me that some records don't have this. That could be why they aren't showing in your grid.

To me reading it, the desires about images probably aren't related to the variables... unless the images are the ID variables? But I have a hard time believing that variable e.empl_id is an image, though it could be their ID badge photo I suppose. Without providing what the classes 'a', 's' and 'e' actually consist of a.anything is just guesswork.

What is it you are asking of the people here? What would cause the aid and sid variable values to not display on your grid? How do you conditionally place a graphic on a grid? Would someone telepathically intuit what my variables are and write the code to fix it for me? Could someone point me at a tutorial about gridviews? Has anyone ever successfully put images on a gridview and if so how? Is there a limit to the number of values I can put on a grid view? Does an image on a gridview have to be in a specific field, like the first column only? Do I have to turn on a specific property to show an image?

As you can see, there a LOT of questions you could be asking. Its not right to make the people here have to guess what it is you are asking.
  #5  
Old March 17th, 2009, 03:33 AM
Familiar Sight
 
Join Date: Feb 2007
Posts: 142

re: gridview - display icon conditionally


Okay, I see your point. I guess I need to be more clear. I notice you and I are not on the same page. SORRY for not being clear.

sid is int field (supervisor id) coming from supervisors table
aid is int field (admin id) coming from admin table.

I am displaying all the records from employee table. I would like put key icon for those employees who belong to admin table. I would like to face with key icon for those employees who belong to supervisor table. Hope it's clear now.

I was trying to convert the sample vb code from the help link I was following earlier but case statement in vb is different than c#, c# does not allow fall through. so i tried if but getting an error. hmm. any help would be appreciated. Here is the class I am trying to build for.

I am getting this error
'System.Web.UI.WebControls.TableRow.Cells' is a 'property' but is used like a 'method' on this line if (((int)e.Row.Cells(0).Text) == null)


Expand|Select|Wrap|Line Numbers
  1.  
  2.         if (e.Row.RowType == DataControlRowType.DataRow)
  3.         {
  4.             Image img = (Image)e.Row.FindControl("Image1");
  5.             if (((int)e.Row.Cells(0).Text) == null)
  6.              {
  7.                 img.Visible = false;
  8.                 break;
  9.              }
  10.              else (((int)e.Row.Cells(0).Text) != null);
  11.             {
  12.                 img.ImageUrl = "/images/admin16.jpg";
  13.                 img.Visible=true;
  14.             }
  15.  
  16.  
  17.         }
  18.  
  #6  
Old March 17th, 2009, 04:51 AM
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,690
Provided Answers: 3

re: gridview - display icon conditionally


Quote:
I was trying to convert the sample vb code from the help link I was following earlier but case statement in vb is different than c#, c# does not allow fall through.
Well, if that's the crux of your problem, we can do something about that. To be more precise.. C# Case does not allow implicit fallthrough, but fallthrough can be made to happen explicitly with one line.

Expand|Select|Wrap|Line Numbers
  1. switch (/*...*/) {
  2.     case 0: // shares the exact same code as case 1
  3.     case 1:
  4.         // do something
  5.         goto case 2;
  6.     case 2:
  7.         // do something else
  8.         goto default;
  9.     default:
  10.         // do something entirely different
  11.         break;
Here case 2 will 'fall-through' to case default because it was told explicitly to go there.

Quote:
I am getting this error
'System.Web.UI.WebControls.TableRow.Cells' is a 'property' but is used like a 'method' on this line if (((int)e.Row.Cells(0).Text) == null)
Change Cells(0) to Cells[0]. Array elements are surrounded by square brackets. Method arguments are surrounded by parenthesis.

Personally I prefer this format for that if comparrison
Expand|Select|Wrap|Line Numbers
  1.                 if (string.IsNullOrEmpty(  (int)e.Row.Cells[0].Text)     ))
  2.                     {
  3.                      ... your code
  4.                     }
  5.  
But as anyone who as read my cookbook can tell you: There is more than one way to skin a cat.
  #7  
Old March 17th, 2009, 06:31 AM
Familiar Sight
 
Join Date: Feb 2007
Posts: 142

re: gridview - display icon conditionally


I get the admin icon for everyrow. hmm. am i not referencing the correct column. the 1st column either has value or null. if has value then display icon.

Expand|Select|Wrap|Line Numbers
  1.     void GridView2_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
  2.     {
  3.         if (e.Row.RowType == DataControlRowType.DataRow)
  4.         {
  5.             Image img = (Image)e.Row.FindControl("Image1");
  6.             //if ((e.Row.Cells[1].Text) == null )
  7.             if (string.IsNullOrEmpty(e.Row.Cells[1].Text))
  8.             {
  9.                 img.Visible = false;                
  10.              }
  11.              else
  12.             {
  13.                // (((int)e.Row.Cells[0].Text) != null);
  14.  
  15.                 img.ImageUrl = "images/admin_32.gif";
  16.                 img.Visible=true;
  17.             }
  18.  
  19.  
  20.         }
  21.     }    
  22.  
  23.  
  24.  
  #8  
Old March 17th, 2009, 07:45 AM
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,690
Provided Answers: 3

re: gridview - display icon conditionally


Put a breakpoint at line 7 and walk through with F-10
Hovering over a bit of code such as the .Text at the end of line 7 will show you the current values. Also helps to have the Auto window open so you can see all the values at that point. I'm going to guess that the string you expect to be empty really isn't.

  #9  
Old March 17th, 2009, 07:53 AM
kunal pawar's Avatar
Needs Regular Fix
 
Join Date: Oct 2007
Location: Pune, India
Posts: 297

re: gridview - display icon conditionally


I got your problem. you want to show icon as per user. If user is superadmin then icon should be superadmin and if user is administrator then icon is administrator

You have change sql query just pass true for superadmin and false for Administrator
and set visible = that column name for images
  #10  
Old March 17th, 2009, 08:45 PM
Familiar Sight
 
Join Date: Feb 2007
Posts: 142

re: gridview - display icon conditionally


Okay, I put a break point and I notice for all the Null values it's showing " " therefore the else part kicks in. hmm, what's the fix for that?
  #11  
Old March 17th, 2009, 08:55 PM
Familiar Sight
 
Join Date: Feb 2007
Posts: 142

re: gridview - display icon conditionally


Okay, this code is working. I had to replace the gridview space with nothing. Here is the final code. I TRUELY appreciate all the help and especially in details. Thanks from the bottom of my heart. You guys are awesome.

Expand|Select|Wrap|Line Numbers
  1.  
  2.     void GridView2_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
  3.     {
  4.         if (e.Row.RowType == DataControlRowType.DataRow)
  5.         {
  6.             Image img = (Image)e.Row.FindControl("Image1");
  7.             if (string.IsNullOrEmpty(e.Row.Cells[1].Text.Replace(" ","")))
  8.             {
  9.                 img.Visible = false;
  10.                 img.ImageUrl = "images/blank.jpg";
  11.              }
  12.              else
  13.             {
  14.                 img.ImageUrl = "images/admin_32.gif";
  15.                 img.Visible=true;
  16.             }
  17.  
  18.  
  19.         }
  20.     }    
  21.  
  22.  
  #12  
Old March 17th, 2009, 09:23 PM
Familiar Sight
 
Join Date: Feb 2007
Posts: 142

re: gridview - display icon conditionally


one last thing. If someone belongs to both admin and super table then admin icon will take priority over supervisor. Here is the code. I comment 2nd part of the if statement. It works.

Expand|Select|Wrap|Line Numbers
  1.             if (string.IsNullOrEmpty(e.Row.Cells[3].Text.Replace(" ","")))
  2.             {
  3.                 img.Visible = false;
  4.                 img.ImageUrl = "images/blank.jpg";
  5.              }
  6.              else
  7.             {
  8.                 img.ImageUrl = "images/super_16.gif";
  9.                 img.Visible=true;
  10.             }
  11.             if (string.IsNullOrEmpty(e.Row.Cells[2].Text.Replace(" ", "")))
  12.             {
  13.                 //img.Visible = false;
  14.                 //img.ImageUrl = "images/blank.jpg";
  15.             }
  16.             else
  17.             {
  18.                 img.ImageUrl = "images/access_16x16.gif";
  19.                 img.Visible = true;
  20.             }
  21.  
  22.  

Last edited by dorandoran; March 17th, 2009 at 09:39 PM. Reason: found the solution and posting it.
  #13  
Old March 18th, 2009, 05:26 AM
Familiar Sight
 
Join Date: Feb 2007
Posts: 142

re: gridview - display icon conditionally


It's all working now. However, if I set visible=false for sid and aid in the gridview then it fails. anyone??
  #14  
Old March 18th, 2009, 07:19 AM
BeemerBiker's Avatar
Member
 
Join Date: Jul 2008
Location: San Antonio, Texas
Posts: 68

re: gridview - display icon conditionally


Quote:
Originally Posted by dorandoran View Post
It's all working now. However, if I set visible=false for sid and aid in the gridview then it fails. anyone??
This may not apply in your case, but I recall that setting something invisible make them not present and they cannot be accessed. I had to set the style of a bound field in a grid to "hideStyle" so I could still access the data in the bound field even though it was not being displayed in the grid. "hideStyle" was simply
.hideStyle
{
display:none;
}

HTH
  #15  
Old March 18th, 2009, 05:02 PM
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,690
Provided Answers: 3

re: gridview - display icon conditionally


Just curious... If the image you are placing is "blank.jpg" then why do you also need to hid the control? Isn't an empty image giving the same look?
  #16  
Old March 19th, 2009, 04:59 AM
Familiar Sight
 
Join Date: Feb 2007
Posts: 142

re: gridview - display icon conditionally


Beember Biker: I used the built-in gridview tool to create my gridview. and I dont see the display:none option in style. I think you were referring to css file which I am not using with my gridview page. I dont mind using it if thats the only option I have. I will need concrete example and snippet for this approach.

tlhintoq: you are right. no need to assign any blank.jpg since it's going 2 b hidden. thanks.
  #17  
Old March 19th, 2009, 05:20 AM
Familiar Sight
 
Join Date: Feb 2007
Posts: 142

re: gridview - display icon conditionally


Found my solution and worked perfectly. Thank you Beember Biker

I followed this and bingo. http://allgoodblogs.com/fredblau/?p=9

Again thanks for beemer biker.
Reply