Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Paging with Datagrid in Asp.net 1.1 and XML

0.00/5 (No votes)
27 Feb 2006 1  
Paging with Datagrid in Asp.net 1.1 and XML

Introduction

Xml is really good has alot of future when use as database.For Example you want to add your guestbook to Xml database and then use datagrid for showing the data.But you want to add pagging to your datagrid.Here I explain pagging in datagrid and Xml file.

Database

The Part will be stored in an XML file on the server, named PartList.xml. The encoding of the Here is the structure of the XML file:

<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema"
   xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="part">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="quantity" type="xs:integer" minOccurs="0" msdata:Ordinal="1" />
          <xs:element name="manufacturer" type="xs:string" minOccurs="0" msdata:Ordinal="2" />
          <xs:element name="color" type="xs:string" minOccurs="0" msdata:Ordinal="3" />
          <xs:element name="price" type="xs:float" minOccurs="0" msdata:Ordinal="4" />
        </xs:sequence>
        <xs:attribute name="partid" type="xs:integer" />
      </xs:complexType>
    </xs:element>
    <xs:element name="NewDataSet" msdata:IsDataSet="true">
      <xs:complexType>
        <xs:choice maxOccurs="unbounded">
          <xs:element ref="part" />
        </xs:choice>
      </xs:complexType>
    </xs:element>
  </xs:schema>
  <part partid="1">
    <quantity>2</quantity>
    <manufacturer>Torux</manufacturer>
    <color>Red</color>
    <price>1</price>
  </part>
  <part partid="2">
    <quantity>0</quantity>
    <manufacturer>Pear</manufacturer>
    <color>white</color>
    <price>99.99</price>
  </part>
  <part partid="3">
    <quantity>34</quantity>
    <manufacturer>Torux</manufacturer>
    <color>black</color>
    <price>22.95</price>
  </part>
  <part partid="4">
    <quantity>3</quantity>
    <manufacturer>Torux</manufacturer>
    <color>clear</color>
    <price>45</price>
  </part>
  <part partid="5">
    <quantity>5</quantity>
    <manufacturer>Torux</manufacturer>
    <color>black</color>
    <price>12</price>
  </part>
  <part partid="6">
    <quantity>87</quantity>
    <manufacturer>Torux</manufacturer>
    <color>black</color>
    <price>11.95</price>
  </part>
  <part partid="7">
    <quantity>0</quantity>
    <manufacturer>Pear</manufacturer>
    <color>white</color>
    <price>655.99</price>
  </part>
  <part partid="8">
    <quantity>65</quantity>
    <manufacturer>Pear</manufacturer>
    <color>white</color>
    <price>299.99</price>
  </part>
  <part partid="9">
    <quantity>5</quantity>
    <manufacturer>Pear</manufacturer>
    <color>white</color>
    <price>299.99</price>
  </part>
  <part partid="10">
    <quantity>0</quantity>
    <manufacturer>Torux</manufacturer>
    <color>black</color>
    <price>1.99</price>
  </part>
  <part partid="11">
    <quantity>45</quantity>
    <manufacturer>MacroWare</manufacturer>
    <color>black</color>
    <price>855</price>
  </part>
  <part partid="12">
    <quantity>6</quantity>
    <manufacturer>Pear</manufacturer>
    <color>black</color>
    <price>566.98</price>
  </part>
  <part partid="13">
    <quantity>0</quantity>
    <manufacturer>Torux</manufacturer>
    <color>black</color>
    <price>4</price>
  </part>
  <part partid="14">
    <quantity>44</quantity>
    <manufacturer>MacroWare</manufacturer>
    <color>black</color>
    <price>188.94</price>
  </part>
  <part partid="15">
    <quantity>1</quantity>
    <manufacturer>Pear</manufacturer>
    <color>white</color>
    <price>99</price>
  </part>
  <part partid="16">
    <quantity>9</quantity>
    <manufacturer>Pear</manufacturer>
    <color>white</color>
    <price>34.43</price>
  </part>
  <part partid="17">
    <quantity>56</quantity>
    <manufacturer>MacroWare</manufacturer>
    <color>black</color>
    <price>19.99</price>
  </part>
  <part partid="18">
    <quantity>534</quantity>
    <manufacturer>MacroWare</manufacturer>
    <color>black</color>
    <price>34.88</price>
  </part>
  <part partid="19">
    <quantity>0</quantity>
    <manufacturer>MacroWare</manufacturer>
    <color>black</color>
    <price>998.99</price>
  </part>
  <part partid="20">
    <quantity>5</quantity>
    <manufacturer>Torux</manufacturer>
    <color>black</color>
    <price>5</price>
  </part>
</NewDataSet>

Application code:

In visual studio.net we open new web page(webform1.aspx) ,then add this code:

<asp:datagrid id="DataGrid1" runat="server" AllowPaging="True" 
    PageSize="5"></asp:datagrid>

And in code behind add this code:

 

The guestbook will be stored in an XML file on the server, named guestbook.xml. The encoding of the XML file is changed to ISO-8859-1 to be able to handle special characters. Here is the structure of the XML file:

Private Sub Page_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
        'Fill the Dataset
        Dim ds As New DataSet()
        ds.ReadXml(Server.MapPath(".") & "\..\Xml\PartList.xml")

        'Set the DataGrid's Source and bind it.
        DataGrid1.DataSource = ds
        If Not IsPostBack Then
            DataGrid1.DataBind()
        End If

        'Dispose Items
        ds.Dispose()
    End Sub

    'Implement the EventHandler for PageIndexChanged
    Private Sub ChangePage(ByVal source As Object, ByVal e As _
    System.Web.UI.WebControls.DataGridPageChangedEventArgs) _
    Handles DataGrid1.PageIndexChanged
        DataGrid1.CurrentPageIndex = e.NewPageIndex
        DataGrid1.DataBind()
    End Sub

ok!now you can test your program.I hope you enjoy!!

Conclusion

I would say that you gain to separate data from processes, and in this matter, XML helps a lot. If you would like to change the looks of the datagrod view, you need only to change the asp.net code file.

 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here