Displaying serial number in DataGrid

Last Modified On : 30-Sep-2006

<%@ Import="" namespace="System.Data" %>
<%@ Import="" namespace="System.Data.SQLClient" %>

<script language="VB" runat="server">

Sub Page_Load (sender as object, e as eventargs)

      Dim con as new SQLConnection("uid=sa;database=pubs")
      con.open()
      dim cmd as new SQLCommand("select title,type, price from titles",con)
      dim dr as Sqldatareader
      dr = cmd.ExecuteReader()
      datagrid1.datasource = dr
      datagrid1.databind()
      dr.close()
      con.close()

End Sub

Sub populateSerialNumber(sender as object, objargs as DataGridItemEventArgs)
      If objArgs.Item.ItemType <> ListItemType.Header Then
         objArgs.Item.Cells(0).text = objArgs.Item.datasetindex + 1
      End If
End sub
</script>


<form Runat="server">
      <Asp:DataGrid Runat="server" id="datagrid1"  OnItemDataBound = "PopulateSerialNumber">
      <Columns>
      <Asp:TemplateColumn HeaderText="S.No" ItemStyle-HorizontalAlign="right">
      <ItemTemplate></ItemTemplate>
      </Asp:TemplateColumn>
      </columns>

      </Asp:Datagrid>
</form>