Identify top 5 rows of a DataGrid

I have the following DataGrid:

<asp:DataGrid id="dgNewsArticles" AutoGenerateColumns="False" CellPadding="1" DataKeyField="RecID"
ShowFooter="false" runat="server">
<Columns>
      <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" />
</Columns>
</asp:DataGrid>

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?
russomrAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jason ScolaroCommented:
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:TemplateColumn>
    <ItemTemplate>
        <asp:Image runat="server" Visible='<%# IIf(Container.ItemIndex <= 4, True, False) %>' ImageUrl="myGraphic.jpg" id="Image1" />
    </ItemTemplate>
</asp:TemplateColumn>

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
russomrAuthor Commented:
I get this as an error:

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

Am I missing a namespace for the IIF?
RainMan82Commented:
its just an if statement....

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

if (Container.ItemIndex <= 4) then
         true
else
          false
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

Jason ScolaroCommented:
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
russomrAuthor Commented:
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?
russomrAuthor Commented:
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.
Jason ScolaroCommented:
russomr,

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;
    else
        return false;
}

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

-- Jason

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
russomrAuthor Commented:
Works!  Thank you.  Sorry for being such a pain.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.