Solved

Autonumber column in GridView

Posted on 2006-11-16
2
825 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 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