Solved

Autonumber column in GridView

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

Technology Partners: 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!

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

740 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