Solved

Autonumber column in GridView

Posted on 2006-11-16
2
827 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

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…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Suggested Courses

752 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