gridview - display icon conditionally 
March 17th, 2009, 01:15 AM
| | Familiar Sight | | Join Date: Feb 2007
Posts: 142
| |
I have a gridview "gridview2" and here is the datasource -
SELECT e.emp_id, e.emp_pin, e.last_name, e.first_name, e.hiring_date, e.active,
-
e.email_address, e.hourly_employee, e.uid, a.administrator_id aid,
-
s.supervisor_id sid
-
FROM EMPLOYEES e LEFT OUTER JOIN
-
SUPERVISORS s ON e.emp_id = s.emp_id LEFT OUTER JOIN
-
ADMINISTRATORS a ON e.emp_id = a.emp_id
-
ORDER BY e.first_name
-
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
| 
March 17th, 2009, 02:38 AM
|  | 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. | 
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?
| 
March 17th, 2009, 03:17 AM
|  | 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: - a.administrator_id aid,
-
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.
| 
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) -
-
if (e.Row.RowType == DataControlRowType.DataRow)
-
{
-
Image img = (Image)e.Row.FindControl("Image1");
-
if (((int)e.Row.Cells(0).Text) == null)
-
{
-
img.Visible = false;
-
break;
-
}
-
else (((int)e.Row.Cells(0).Text) != null);
-
{
-
img.ImageUrl = "/images/admin16.jpg";
-
img.Visible=true;
-
}
-
-
-
}
-
| 
March 17th, 2009, 04:51 AM
|  | 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. - switch (/*...*/) {
-
case 0: // shares the exact same code as case 1
-
case 1:
-
// do something
-
goto case 2;
-
case 2:
-
// do something else
-
goto default;
-
default:
-
// do something entirely different
-
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 - if (string.IsNullOrEmpty( (int)e.Row.Cells[0].Text) ))
-
{
-
... your code
-
}
-
But as anyone who as read my cookbook can tell you: There is more than one way to skin a cat.
| 
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. -
void GridView2_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
-
{
-
if (e.Row.RowType == DataControlRowType.DataRow)
-
{
-
Image img = (Image)e.Row.FindControl("Image1");
-
//if ((e.Row.Cells[1].Text) == null )
-
if (string.IsNullOrEmpty(e.Row.Cells[1].Text))
-
{
-
img.Visible = false;
-
}
-
else
-
{
-
// (((int)e.Row.Cells[0].Text) != null);
-
-
img.ImageUrl = "images/admin_32.gif";
-
img.Visible=true;
-
}
-
-
-
}
-
}
-
-
-
| 
March 17th, 2009, 07:45 AM
|  | 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.  | 
March 17th, 2009, 07:53 AM
|  | 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
| 
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?
| 
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. -
-
void GridView2_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
-
{
-
if (e.Row.RowType == DataControlRowType.DataRow)
-
{
-
Image img = (Image)e.Row.FindControl("Image1");
-
if (string.IsNullOrEmpty(e.Row.Cells[1].Text.Replace(" ","")))
-
{
-
img.Visible = false;
-
img.ImageUrl = "images/blank.jpg";
-
}
-
else
-
{
-
img.ImageUrl = "images/admin_32.gif";
-
img.Visible=true;
-
}
-
-
-
}
-
}
-
-
| 
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. -
if (string.IsNullOrEmpty(e.Row.Cells[3].Text.Replace(" ","")))
-
{
-
img.Visible = false;
-
img.ImageUrl = "images/blank.jpg";
-
}
-
else
-
{
-
img.ImageUrl = "images/super_16.gif";
-
img.Visible=true;
-
}
-
if (string.IsNullOrEmpty(e.Row.Cells[2].Text.Replace(" ", "")))
-
{
-
//img.Visible = false;
-
//img.ImageUrl = "images/blank.jpg";
-
}
-
else
-
{
-
img.ImageUrl = "images/access_16x16.gif";
-
img.Visible = true;
-
}
-
-
Last edited by dorandoran; March 17th, 2009 at 09:39 PM.
Reason: found the solution and posting it.
| 
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??
| 
March 18th, 2009, 07:19 AM
|  | Member | | Join Date: Jul 2008 Location: San Antonio, Texas
Posts: 68
| | | re: gridview - display icon conditionally Quote:
Originally Posted by dorandoran 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
| 
March 18th, 2009, 05:02 PM
|  | 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?
| 
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.
| 
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.
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,662 network members.
|