Solved

Autonumber column in GridView

Posted on 2006-11-16
2
822 Views
Last Modified: 2011-10-03
I have a simple gridview that displays a "Top 10" list. The list is ordered by a Rank column in the db, but this column doesn't necessarily have 1-10 listings. it might go 245, 256, 280, 290.. etc.

Is there a easy, easy, simple way to have a template field that just does a 1 to 10 autonumber type thing? This was easy as pie in ASP.Classic, a little looped number generation, but in C# and .Net 2.0, I'm hitting a roadblock.
0
Comment
Question by:Paul Kahl
2 Comments
 
LVL 6

Expert Comment

by:so3
ID: 17959083
when you fill the dataset/datatable from db ordered by rank in the select use top 10 statement
Example: SELECT TOP 10 column1, column2 from mydb order by RANK ASC

this will get the first 10 listings and you don't have to modify anything in gridview

0
 
LVL 8

Accepted Solution

by:
redpipe earned 500 total points
ID: 17960915
As I understand your question you want to display the 1-10 number listings in the grid. Fool about with the following code that applies the current row number to the rowheadercell (the grey leftmost item in the grid).

public static void applyRowNumbersToGrid(DataGridView grid)
{
      int rowNumber = 1;
      grid.RowHeadersVisible = true;
      grid.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;
      foreach (DataGridViewRow row in grid.Rows) {
        if (row.IsNewRow) continue;
        row.HeaderCell.Value = Convert.ToString(rowNumber);
        rowNumber++;
      }
}
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now