Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Autonumber column in GridView

Posted on 2006-11-16
2
Medium Priority
?
830 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

715 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