<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> <Asp:TemplateColumn HeaderText="S.No" ItemStyle-HorizontalAlign="right">
<ItemTemplate>
</ItemTemplate>
</Asp:TemplateColumn></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 <> 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><%@ Page Language="VB" Debug="True" %>
<%@ Import namespace="System.Data" %>
<%@ Import namespace="System.Data.Oledb" %>
<script language="VB" runat="server">
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 <> ListItemType.Header Then
<STRONG>objArgs.Item.Cells(0).text = objArgs.Item.datasetindex + 1</STRONG>
End If
End sub
</script>
<form name="ListOpenQuestion" id="ListOpenQuestion" Runat="server">
<Asp:DataGrid Runat="server" id="dtgrid"
<STRONG>OnItemDataBound = "populateSerialNumber"</STRONG>
AutoGenerateColumns=false
Width="75%"
Align="center">
<Columns>
<STRONG><Asp:TemplateColumn HeaderText="S.No" ItemStyle-HorizontalAlign="right">
<ItemTemplate>
</ItemTemplate>
</Asp:TemplateColumn></STRONG>
<Asp:HyperLinkColumn DataTextField="qtitle"
DataNavigateUrlFormatstring="reply.aspx?id={0}"
DataNavigateUrlField="qid" HeaderText="Question Title"/>
<Asp:BoundColumn DataField="qDate"
HeaderText="Date Posted"
ItemStyle-HorizontalAlign="right" />
<Asp:HyperLinkColumn DataTextField="email"
DataNavigateUrlFormatString="mailto:{0}"
DataNavigateUrlField="email"
HeaderText="Posted By"/>
</Columns>
</Asp:DataGrid>
</form>
</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>
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More ASP.NET Code Articles
More By aspfree
developerWorks - FREE Tools! |
<a href="http://zeus.developershed.com/shonuff.php?blackbird=3853&zoneid=442&source=&dest=http%3A%2F%2Fwww.ibm.com%2Fdeveloperworks%2Fspaces%2Fjazz%3FS_TACT%3D105AGY31%26S_CMP%3DDEVSHED&ismap="><img src="http://images.devshed.com/corp/img/news/jazz01.gif" alt="developerWorks Jazz space" align="left"></a>You've heard the buzz about Jazz... want to know more about it from a developer's perspective? Check out the Jazz space on developerWorks. This space is an up-to-date resource for developers, including technical information about Jazz and products built on Jazz, like Rational Team Concert Express. The Jazz space includes content from a wide variety of sources, including links, feeds, and comments from experts. FREE! Go There Now!
|
|
|
|
As businesses grow increasingly dependent upon Web applications to provide services to customers, employees and partners, these complex applications become more difficult to secure. Although traditional security solutions protect Internet infrastructure layers, they do not guard against HTTP and HTML attacks. Many organizations that conduct security testing still deploy applications that allow attackers to manipulate their logic and wreak havoc on their business. To mitigate this risk, development and delivery teams must address Web application security throughout the lifecycle, addressing the many layers detailed in this paper. FREE! Go There Now!
|
|
|
|
Join us for this on demand webcast to learn about developing complex systems more quickly and efficiently. We'll cover market drivers for developing, governing and reusing systems software assets and how you can develop system software assets with Rational Asset Manager. FREE! Go There Now!
|
|
|
|
Download the Rational Application Developer (RAD) v7.5 open beta code and start developing applications for the JEE5 standard which features EJB3.0, JPA, JSF 1.2, JSP 2.1 and Servlet 2.5 standards. When you use this beta you will see how you can increase developer productivity for already existing applications with improved support for refactoring, as well as adding new features to existing applications. In addition, the beta provides tooling for JD Edwards, Oracle, SAP, Siebel and PeopleSoft to improve the developer productivity with these enterprise systems. FREE! Go There Now!
|
|
|
|
Discover how IBM Rational AppScan Standard Edition can help you detext vulnerabilities in your web applications in the Web Application Security eKit. IBM Rational AppScan is a leading suite of automated web application security solutions that scan and test for common Web application vulnerabilities. The new Web Application Security eKit provides you with valuable resources, including white papers, demos, and additional information on the benefits of testing your Web applications. FREE! Go There Now!
|
|
|
|
Portfolio Management is about effectively managing portfolio value by aligning portfolio investments with business goals. This complimentary e-kit provides a collection of materials that can help you understand how IBM Rational enables and automates best practices for improved governance and clear visibility into portfolio and project performance across the entire IT project lifecycle. FREE! Go There Now!
|
|
|
|
Join this webcast to discover the key requirements for successful change and release management. Learn how to extend your .NET environment to improve productivity and collaboration, and address core problems afflicting team development. In this webcast, we’ll review typical challenges faced by customers and how to resolve them with the IBM Rational Change and Release Management solution, including Rational ClearCase, Rational ClearQuest and Rational Build Forge. Replay is available for 9 months. FREE! Go There Now!
|
|
|
|
Join this Rational Talks to You teleconference, to hear how Enterprise Generation Language (EGL) eliminates the need for tedious and error-prone low level coding, so developers can focus on business requirements. EGL extends the Rational software development platform with a simplified programming language that enables developers who have little or no experience with Java, Web technologies or Service Oriented Architecture, to create enterprise-class applications and services quickly and easily. It also allows developers who may have little or no mainframe programming experience to quickly create traditional mainframe components. FREE! Go There Now!
|
|
|
|
The discipline of assembling and delivering software is maturing beyond standard developer-centric compile/test software builds. The end-to-end software development lifecycle is emerging as the new focus moves “Beyond the Build.” Join this on demand webcast to learn about methods for streamlining software delivery and key capabilities of the IBM Rational Build Forge framework for automating build and release management in environments of any size. FREE! Go There Now!
|
|
|
|
The unprecedented scope of a service-oriented architecture (SOA) initiative brings to the forefront a number of management and governance issues that were sidestepped in the past. The key to a successful SOA implementation is managing and governing activities throughout the entire SOA delivery lifecycle by ensuring that services conform to the needs of all of the business’s stakeholders. Learn how service lifecycle management allows the business to ensure that the process by which services are defined, created, tested, deployed, optimized and retired is manageable, repeatable and auditable. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |