Displaying Serial numbers for rows in DataGrid.

Contributed by
Rating: 4 stars4 stars4 stars4 stars4 stars / 20
October 13, 2002
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement


<P><FONT size=1>All the list controls have an event by name "OnItemDataBound".
We have to make use of this to create a serial number column or autogenerate
numbers. <BR><BR>The basic steps which we need to keep in mind are as follows:
<BR><BR><STRONG>Step1:</STRONG> Create a template column in the datagrid
control. <BR></FONT></P>
<PRE><FONT size=1>     &lt;Asp:TemplateColumn HeaderText="S.No" ItemStyle-HorizontalAlign="right"&gt;

        &lt;ItemTemplate&gt;       

        &lt;/ItemTemplate&gt;

     &lt;/Asp:TemplateColumn&gt;</FONT></PRE><FONT
size=1><STRONG>Step2:</STRONG> For OnItemDataBound assign an event name. That
event would fire when an Item is databound to our datagrid. </FONT>
<BLOCKQUOTE><FONT size=1><EM>For ex:</EM> OnItemDataBound =
"populateSerialNumber"</FONT></BLOCKQUOTE><FONT size=1><STRONG>Step3:</STRONG>
Write the code which would create the serial number with in the procedure
"populateSerialNumber". </FONT>
<PRE><FONT size=1>    Sub populateSerialNumber(sender as object, objargs as DataGridItemEventArgs)

       If objArgs.Item.ItemType &lt;&gt; ListItemType.Header Then

         objArgs.Item.Cells(0).text = objArgs.Item.datasetindex + 1

       End If 

    End sub

</FONT></PRE><FONT size=1><STRONG>The complete
code listing follows:</STRONG></FONT>
<P><FONT size=1>Pay particular attention to the
<STRONG>populateSerialNumber</STRONG> procedure as discussed in step3. Note that
I have specified datasetindex + 1 in the procedure below. This is because
datasetindex value starts from 0. The "If" statement exists to ensure that our
labels (row serial numbers) make more sense and to avoid any text from being
displayed on the header. </FONT></P>
<PRE><FONT size=1>&lt;%@ Page Language="VB" Debug="True" %&gt;

&lt;%@ Import namespace="System.Data" %&gt;

&lt;%@ Import namespace="System.Data.Oledb" %&gt;

 

&lt;script language="VB" runat="server"&gt;

    Sub Page_Load (sender as object, e as eventargs)

       BindAccess()

    End sub 

   

    Sub BindAccess()

       Dim strconn as string

       Dim dtr as new oledbdataAdapter

       dim ds as new dataset

       strconn="PROVIDER=MICROSOFT.JET.OLEdb.4.0;DATA SOURCE="path of your database"

     

       Dim cns as new oledbconnection(strconn)

       cns.open()

     

       strconn = "select title, qdate, email from tablename"

       dtr = new oledbdataAdapter(strconn,cns)

       dtr.fill(ds)

       dtgrid.datasource = ds

       dtgrid.databind()

    End Sub

 

    Sub populateSerialNumber(sender as object, objargs as DataGridItemEventArgs)

       If objArgs.Item.ItemType &lt;&gt; ListItemType.Header Then

         <STRONG>objArgs.Item.Cells(0).text = objArgs.Item.datasetindex + 1</STRONG>

       End If 

    End sub

&lt;/script&gt;

 

&lt;form name="ListOpenQuestion" id="ListOpenQuestion" Runat="server"&gt;

    &lt;Asp:DataGrid Runat="server" id="dtgrid"

            <STRONG>OnItemDataBound = "populateSerialNumber"</STRONG>

            AutoGenerateColumns=false

            Width="75%"

            Align="center"&gt;

    &lt;Columns&gt;

 

      <STRONG>&lt;Asp:TemplateColumn HeaderText="S.No" ItemStyle-HorizontalAlign="right"&gt;

         &lt;ItemTemplate&gt;       

         &lt;/ItemTemplate&gt;

       &lt;/Asp:TemplateColumn&gt;</STRONG>

 

       &lt;Asp:HyperLinkColumn DataTextField="qtitle"

               DataNavigateUrlFormatstring="reply.aspx?id={0}"

               DataNavigateUrlField="qid" HeaderText="Question Title"/&gt;

 

       &lt;Asp:BoundColumn DataField="qDate"

               HeaderText="Date Posted"

               ItemStyle-HorizontalAlign="right" /&gt;

 

       &lt;Asp:HyperLinkColumn DataTextField="email"

               DataNavigateUrlFormatString="mailto:{0}"

               DataNavigateUrlField="email"

               HeaderText="Posted By"/&gt;

    &lt;/Columns&gt;

    &lt;/Asp:DataGrid&gt;

&lt;/form&gt;

</FONT></PRE>
<P><FONT size=1>* To test the above code with SQL Server (this code incidentally
uses Ms Access as the datasource), replace all occurances of "oledb" with "sql"
(Replace "System.Data.Oledb" with "System.Data.SQLClient"). You will need to
make suitable changes to the connection string (strconn) as well.</FONT></P>

blog comments powered by Disqus
ASP.NET CODE ARTICLES

- How to Use the ListBox Control in ASP.NET 2.0
- How to Load XML Documents in ASP.NET 2.0
- DataGrid Code
- ASP.NET Guestbook
- User Controls and Client Side Scripting
- ASP.NET Programming with Microsoft's AS...
- ASP.NET Basics (part 3): Hard Choices
- ASP.NET Basics (part 2): Not My Type
- ASP.NET Basics (part 1): Nothing But .Net
- Directory Tree Browser
- How to get the confirmation of Yes/No from a...
- Complete example using custom errors and wri...
- Paging Certain # records per page .NET style
- General Methods of formatting and Subtractin...
- .NET LinkButton web control

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 8 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials