Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Add index in repeater/datalist/datagrid

Add index in repeater/datalist/datagrid

Scheduled Pinned Locked Moved ASP.NET
databasetutorialquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rain1178
    wrote on last edited by
    #1

    Any one know how to add an index column in repeater / data list or datagrid? Thank you!

    R J 2 Replies Last reply
    0
    • R rain1178

      Any one know how to add an index column in repeater / data list or datagrid? Thank you!

      R Offline
      R Offline
      Raymond Ooi
      wrote on last edited by
      #2

      The ItemDataBound of the DataGrid occurs when data is bound to a item in a DataGrid control. The first step is to create an empty TemplateColumn in the DataGrid control as follows: ................. The next step is to specify an event handler for the DataGrid's ItemDataBound using the following event signature. void eventhandlername(object Sender, DataGridItemEventArgs e) { ......................... } This event handler should be mapped to the OnItemDataBound property of the DataGrid control as follows: The DataGridItemEventArgs has a property called Item which is of type DataGridItem and gets the referenced item in the DataGrid when the event happens. DataGridItem has a property called DataSetIndex which gives the index number of the DataGridItem. We will have to display this number within the empty template column as follows: e.Item.Cells[0].Text= e.Item.DataSetIndex + 1; Since the DataSetIndex starts with zero one is added to it for displaying the serial number. Also we will have to check the ItemType and display serial number only if the item type is not a Header or a Footer. if(e.Item.Itemtype != ListItemType.Header && e.Item.Itemtype != ListItemType.Footer) { e.Item.Cells[0].Text= e.Item.DataSetIndex + 1; }

      1 Reply Last reply
      0
      • R rain1178

        Any one know how to add an index column in repeater / data list or datagrid? Thank you!

        J Offline
        J Offline
        Jim MacDonald 0
        wrote on last edited by
        #3

        I think the following will work as well:

        R 1 Reply Last reply
        0
        • J Jim MacDonald 0

          I think the following will work as well:

          R Offline
          R Offline
          rain1178
          wrote on last edited by
          #4

          Thanks to Raymond and Jim MacDonald!:rose:

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups