Identify top 5 rows of a DataGrid

Posted on 2006-03-21
Medium Priority
Last Modified: 2008-02-01
I have the following DataGrid:

<asp:DataGrid id="dgNewsArticles" AutoGenerateColumns="False" CellPadding="1" DataKeyField="RecID"
ShowFooter="false" runat="server">
      <asp:BoundColumn HeaderText="RecID" DataField="RecID" Visible="False" />
      <asp:BoundColumn DataField="DisplayAs" HeaderText="News Article" />
      <asp:BoundColumn DataField="PostDate" HeaderText="Post Date" />
      <asp:HyperLinkColumn ItemStyle-Width="50px" DataNavigateUrlField="RecID" DataNavigateUrlFormatString="editFile.aspx?RecID={0}" Text="Edit" />
      <asp:HyperLinkColumn ItemStyle-Width="50px" DataNavigateUrlField="RecID" DataNavigateUrlFormatString="deleteFile.aspx?RecID={0}" Text="Delete" />

When displaying this grid on the screen, I would like to note the top 5 rows by adding a column at the beginning with an icon in it.

Any thoughts?
Question by:russomr
  • 4
  • 3
LVL 22

Expert Comment

by:Jason Scolaro
ID: 16246503
Hi russomr,

First of all, you can't really make a column specifically for the first 5 rows, but you can generate a column for the entire Grid and then only show an image for the first 5 rows.  There are some alternatives, but I think this is the easiest...

Now there are multiple ways to implement this following solution, but try it this way first, add a new column:

        <asp:Image runat="server" Visible='<%# IIf(Container.ItemIndex <= 4, True, False) %>' ImageUrl="myGraphic.jpg" id="Image1" />

This creates a new column and only shows the image for the first 5 rows, otherwise it hides the Image (doesn't render it).  Good luck!

-- Jason

Author Comment

ID: 16246783
I get this as an error:

The name 'IIf' does not exist in the current context

Am I missing a namespace for the IIF?

Expert Comment

ID: 16246806
its just an if statement....

IIf(Container.ItemIndex <= 4, True, False) =

if (Container.ItemIndex <= 4) then
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 22

Expert Comment

by:Jason Scolaro
ID: 16246820
Are you using C#?  If so... this would be it:

<asp:Image runat="server" Visible='<%# ( (bool) ((int)Container.ItemIndex) <= 4 ? true : false) %>' ImageUrl="myGraphic.jpg" id="Image1" />

Or something close to that... I do just about everything in VB.NET... but that will work.

-- Jason

Author Comment

ID: 16246852
I understand the concept, I just can't get it working.  It looks like IIf is a function that you are referencing.  Where is that function located?  Or where do I create that function so that it can be referenced?

Am I overthinking this?

Author Comment

ID: 16246983
Sorry, I just can't get this to work.  I keep getting

Cannot convert type 'int' to 'bool'

I am a VB programmer that is just now getting into C#, so syntax is not my cup of tea.

Is there a different way to accomplish what I'm going for?  I see how the above would work, but if I can't actually get it working, it does me no good.
LVL 22

Accepted Solution

Jason Scolaro earned 500 total points
ID: 16247042

The IIf is specific to VB.  So you'll have to go another route, either with the ternary operator that I gave you, or let's go another route, unless some C# expert can assist us...

We'll use a Function:

public bool ShowImage(int index) {
    if (index <= 4)
        return true;
        return false;

<asp:Image runat="server" Visible='<%# ShowImage(Container.ItemIndex) %>' ImageUrl="myGraphic.jpg" id="Image1" />

-- Jason

Author Comment

ID: 16247079
Works!  Thank you.  Sorry for being such a pain.

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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.…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Loops Section Overview
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

839 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