Binding Data Using ListView and GridView in ASP.NET without Database

This article shows how to bind data in data controls without using any database and also shows paging on controls. We will learn here how to bind controls with data without a database and how to handle paging on controls.

Initial Chamber


Step 1

Open your Visual Studio and create an empty website then provide a suitable name such as DataControlExp.

Step 2

In Solution Explorer you will get your empty website, then add some web forms.

DataControlExp (your empty website). Right-click and select Add New Item Web Form. Name it DataControlExp.aspx.

Design Chamber

Step 3

Open the DataControlExp.aspx file and write some code for the design of the application.

This is the ListView design phase.

Here I've designed the ListView Control and used some property as in the following:

  1. <div>  
  2.     <h3>ListView</h3>  
  3.     <asp:ListView ID="lvCustomers" runat="server" GroupPlaceholderID="groupPlaceHolder1"  
  4. ItemPlaceholderID="itemPlaceHolder1" OnPagePropertiesChanging="OnPagePropertiesChanging">  
  5.         <LayoutTemplate>  
  6.             <table border="1">  
  7.                 <tr>  
  8.                     <th>Product  </th>  
  9.                     <th>Quantity  </th>  
  10.                     <th>Price  </th>  
  11.                 </tr>  
  12.                 <asp:PlaceHolder runat="server" ID="groupPlaceHolder1"></asp:PlaceHolder>  
  13.                 <tr>  
  14.                     <td colspan="3">  
  15.                         <asp:DataPager ID="DataPager1" runat="server" PagedControlID="lvCustomers" PageSize="2">  
  16.                             <Fields>  
  17.                                 <asp:NextPreviousPagerField ButtonType="Link" ShowFirstPageButton="false" ShowPreviousPageButton="true"  
  18. ShowNextPageButton="false" />  
  19.                                 <asp:NumericPagerField ButtonType="Link" />  
  20.                                 <asp:NextPreviousPagerField ButtonType="Link" ShowNextPageButton="true" ShowLastPageButton="false" ShowPreviousPageButton="false" />  
  21.                             </Fields>  
  22.                         </asp:DataPager>  
  23.                     </td>  
  24.                 </tr>  
  25.             </table>  
  26.         </LayoutTemplate>  
  27.         <GroupTemplate>  
  28.             <tr>  
  29.                 <asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder>  
  30.             </tr>  
  31.         </GroupTemplate>  
  32.         <ItemTemplate>  
  33.             <td><%# Eval("Product_Name") %>  
  34.             </td>  
  35.             <td><%# Eval("Quantity") %>  
  36.             </td>  
  37.             <td><%# Eval("Price") %>  
  38.             </td>  
  39.         </ItemTemplate>  
  40.     </asp:ListView>  
  41. </div> 

FormView control

I've designed a FormView control as in the following:

  1. <h3>FormView</h3>  
  2. <div>  
  3.     <asp:FormView ID="FormView1" runat="server"  
  4. AllowPaging="True" EnableViewState="False" OnPageIndexChanging="FormView1_PageIndexChanging" BackColor="Green">  
  5.         <ItemTemplate>  
  6.             <hr />  
  7.             <h3><%# Eval("Product_Name") %>  
  8.             </h3>  
  9.             <table border="0">  
  10.                 <tr>  
  11.                     <td class="ProductPropertyLabel">Quantity:</td>  
  12.                     <td class="ProductPropertyValue"><%# Eval("Quantity") %>  
  13.                     </td>  
  14.                     <td class="ProductPropertyLabel">Price:</td>  
  15.                     <td class="ProductPropertyValue"><%# Eval("Price")%>  
  16.                     </td>  
  17.                 </tr>  
  18.             </table>  
  19.             <hr />  
  20.         </ItemTemplate>  
  21.     </asp:FormView>  
  22. </div>  
  23. Here design DetailsView-  
  24.   
  25. <h3>DetailsView</h3>  
  26. <div>  
  27.     <div>  
  28.         <asp:DetailsView ID="DetailsView1" OnPageIndexChanging="DetailsView1_PageIndexChanging" BackColor="Wheat"  
  29. AllowPaging="true" runat="server" />  
  30.     </div>  
  31. </div>  
  32. Here design GridView-  
  33.   
  34. <h3>GridView</h3>  
  35. <div>  
  36.     <asp:GridView ID="gvExample" runat="server" AllowPaging="true" PageSize="2" AutoGenerateColumns="False" BackColor="PINK" BorderColor="#E7E7FF"  
  37. BorderWidth="1px" CellPadding="3" OnPageIndexChanging="gvExample_PageIndexChanging">  
  38.         <Columns>  
  39.             <asp:TemplateField HeaderText="Product_Name">  
  40.                 <ItemTemplate>  
  41.                     <asp:Label ID="lblProduct" runat="server" Text='<%#Eval("Product_Name")%>'>  
  42.                     </asp:Label>  
  43.                 </ItemTemplate>  
  44.             </asp:TemplateField>  
  45.             <asp:TemplateField HeaderText="Quantity">  
  46.                 <ItemTemplate>  
  47.                     <asp:Label ID="lblQuantity" runat="server" Text='<%#Eval("Quantity")%>'>  
  48.                     </asp:Label>  
  49.                 </ItemTemplate>  
  50.             </asp:TemplateField>  
  51.             <asp:TemplateField HeaderText="Price">  
  52.                 <ItemTemplate>  
  53.                     <asp:Label ID="lblPrice" runat="server" Text='<%#Eval("Price")%>'>  
  54.                     </asp:Label>  
  55.                 </ItemTemplate>  
  56.             </asp:TemplateField>  
  57.         </Columns>  
  58.     </asp:GridView>  
  59. </div>  
The page looks as in the following.

DataControlExp.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataControlExp.aspx.cs" Inherits="DataControlExp" %>  
  2. <!DOCTYPE html>  
  3. <html  
  4.     xmlns="http://www.w3.org/1999/xhtml">  
  5.     <head runat="server">  
  6.         <title></title>  
  7.     </head>  
  8.     <body>  
  9.         <form id="form1" runat="server">  
  10.             <div>  
  11.                 <div style="width: 50%">  
  12.                     <div>  
  13.                         <h3>ListView</h3>  
  14.                         <asp:ListView ID="lvCustomers" runat="server" GroupPlaceholderID="groupPlaceHolder1"  
  15. ItemPlaceholderID="itemPlaceHolder1" OnPagePropertiesChanging="OnPagePropertiesChanging">  
  16.                             <LayoutTemplate>  
  17.                                 <table border="1">  
  18.                                     <tr>  
  19.                                         <th>Product  
  20. </th>  
  21.                                         <th>Quantity  
  22. </th>  
  23.                                         <th>Price  
  24. </th>  
  25.                                     </tr>  
  26.                                     <asp:PlaceHolder runat="server" ID="groupPlaceHolder1"></asp:PlaceHolder>  
  27.                                     <tr>  
  28.                                         <td colspan="3">  
  29.                                             <asp:DataPager ID="DataPager1" runat="server" PagedControlID="lvCustomers" PageSize="2">  
  30.                                                 <Fields>  
  31.                                                     <asp:NextPreviousPagerField ButtonType="Link" ShowFirstPageButton="false" ShowPreviousPageButton="true"  
  32. ShowNextPageButton="false" />  
  33.                                                     <asp:NumericPagerField ButtonType="Link" />  
  34.                                                     <asp:NextPreviousPagerField ButtonType="Link" ShowNextPageButton="true" ShowLastPageButton="false" ShowPreviousPageButton="false" />  
  35.                                                 </Fields>  
  36.                                             </asp:DataPager>  
  37.                                         </td>  
  38.                                     </tr>  
  39.                                 </table>  
  40.                             </LayoutTemplate>  
  41.                             <GroupTemplate>  
  42.                                 <tr>  
  43.                                     <asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder>  
  44.                                 </tr>  
  45.                             </GroupTemplate>  
  46.                             <ItemTemplate>  
  47.                                 <td><%# Eval("Product_Name") %>  
  48.                                 </td>  
  49.                                 <td><%# Eval("Quantity") %>  
  50.                                 </td>  
  51.                                 <td><%# Eval("Price") %>  
  52.                                 </td>  
  53.                             </ItemTemplate>  
  54.                         </asp:ListView>  
  55.                     </div>  
  56.                     <br />  
  57.                     <h3>FormView</h3>  
  58.                     <div>  
  59.                         <asp:FormView ID="FormView1" runat="server"  
  60. AllowPaging="True" EnableViewState="False" OnPageIndexChanging="FormView1_PageIndexChanging" BackColor="Green">  
  61.                             <ItemTemplate>  
  62.                                 <hr />  
  63.                                 <h3><%# Eval("Product_Name") %>  
  64.                                 </h3>  
  65.                                 <table border="0">  
  66.                                     <tr>  
  67.                                         <td class="ProductPropertyLabel">Quantity:</td>  
  68.                                         <td class="ProductPropertyValue"><%# Eval("Quantity") %>  
  69.                                         </td>  
  70.                                         <td class="ProductPropertyLabel">Price:</td>  
  71.                                         <td class="ProductPropertyValue"><%# Eval("Price")%>  
  72.                                         </td>  
  73.                                     </tr>  
  74.                                 </table>  
  75.                                 <hr />  
  76.                             </ItemTemplate>  
  77.                         </asp:FormView>  
  78.                     </div>  
  79.                 </div>  
  80.                 <div style="width: 50%">  
  81.                     <h3>DetailsView</h3>  
  82.                     <div>  
  83.                         <div>  
  84.                             <asp:DetailsView ID="DetailsView1" OnPageIndexChanging="DetailsView1_PageIndexChanging" BackColor="Wheat"  
  85. AllowPaging="true" runat="server" />  
  86.                         </div>  
  87.                     </div>  
  88.                     <h3>GridView</h3>  
  89.                     <div>  
  90.                         <asp:GridView ID="gvExample" runat="server" AllowPaging="true" PageSize="2" AutoGenerateColumns="False" BackColor="PINK" BorderColor="#E7E7FF"  
  91. BorderWidth="1px" CellPadding="3" OnPageIndexChanging="gvExample_PageIndexChanging">  
  92.                             <Columns>  
  93.                                 <asp:TemplateField HeaderText="Product_Name">  
  94.                                     <ItemTemplate>  
  95.                                         <asp:Label ID="lblProduct" runat="server" Text='<%#Eval("Product_Name")%>'>  
  96.                                         </asp:Label>  
  97.                                     </ItemTemplate>  
  98.                                 </asp:TemplateField>  
  99.                                 <asp:TemplateField HeaderText="Quantity">  
  100.                                     <ItemTemplate>  
  101.                                         <asp:Label ID="lblQuantity" runat="server" Text='<%#Eval("Quantity")%>'>  
  102.                                         </asp:Label>  
  103.                                     </ItemTemplate>  
  104.                                 </asp:TemplateField>  
  105.                                 <asp:TemplateField HeaderText="Price">  
  106.                                     <ItemTemplate>  
  107.                                         <asp:Label ID="lblPrice" runat="server" Text='<%#Eval("Price")%>'>  
  108.                                         </asp:Label>  
  109.                                     </ItemTemplate>  
  110.                                 </asp:TemplateField>  
  111.                             </Columns>  
  112.                         </asp:GridView>  
  113.                     </div>  
  114.                 </div>  
  115.             </div>  
  116.         </form>  
  117.     </body>  
  118. </html>  
CODE CHAMBER

Step 4

In the code chamber we will write some code so that our application works.

DataControlExp.aspx.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Data;  
  4. using System.Data.SqlClient;  
  5. using System.Linq;  
  6. using System.Web;  
  7. using System.Web.UI;  
  8. using System.Web.UI.WebControls;  
  9.   
  10.   
  11. public partial class DataControlExp: System.Web.UI.Page   
  12. {  
  13.     protected void Page_Load(object sender, EventArgs e)   
  14.     {  
  15.         if (!IsPostBack) {  
  16.             BindListView();  
  17.             BindFormView();  
  18.             BindDetailsView();  
  19.             BindGridView();  
  20.         }  
  21.   
  22.     }  
  23.   
  24.     protected void BindListView()   
  25.     {  
  26.         //Creating a dataset  
  27.         DataSet ds = new DataSet();  
  28.         DataTable dt;  
  29.         DataRow dr;  
  30.         DataColumn pName;  
  31.         DataColumn pQty;  
  32.         DataColumn pPrice;  
  33.         //create an object of datatable  
  34.         dt = new DataTable();  
  35.         //creating column of datatable with datatype  
  36.         pName = new DataColumn("Product_Name", Type.GetType("System.String"));  
  37.         pQty = new DataColumn("Quantity", Type.GetType("System.Int32"));  
  38.         pPrice = new DataColumn("Price", Type.GetType("System.Int32"));  
  39.         //bind data table columns in datatable  
  40.         dt.Columns.Add(pName);  
  41.         dt.Columns.Add(pQty);  
  42.         dt.Columns.Add(pPrice);  
  43.         //creating data row and assiging the value to columns of datatable  
  44.         dr = dt.NewRow();  
  45.         dr["Product_Name"] = "Product 1";  
  46.         dr["Quantity"] = 2;  
  47.         dr["Price"] = 200;  
  48.         dt.Rows.Add(dr);  
  49.   
  50.         dr = dt.NewRow();  
  51.         dr["Product_Name"] = "Product 2";  
  52.         dr["Quantity"] = 5;  
  53.         dr["Price"] = 480;  
  54.         dt.Rows.Add(dr);  
  55.   
  56.         dr = dt.NewRow();  
  57.         dr["Product_Name"] = "Product 3";  
  58.         dr["Quantity"] = 8;  
  59.         dr["Price"] = 100;  
  60.         dt.Rows.Add(dr);  
  61.   
  62.         dr = dt.NewRow();  
  63.         dr["Product_Name"] = "Product 4";  
  64.         dr["Quantity"] = 2;  
  65.         dr["Price"] = 500;  
  66.         dt.Rows.Add(dr);  
  67.         //Add datatable to the dataset  
  68.         ds.Tables.Add(dt);  
  69.         //bind data to data controls  
  70.         lvCustomers.DataSource = ds.Tables[0];  
  71.         lvCustomers.DataBind();  
  72.     }  
  73.     //paging code  
  74.     protected void OnPagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)   
  75.     {  
  76.         (lvCustomers.FindControl("DataPager1"as DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, false);  
  77.         this.BindListView();  
  78.     }  
  79.   
  80.     protected void BindFormView()   
  81.     {  
  82.   
  83.         //Creating a dataset  
  84.         DataSet ds = new DataSet();  
  85.         DataTable dt;  
  86.         DataRow dr;  
  87.         DataColumn pName;  
  88.         DataColumn pQty;  
  89.         DataColumn pPrice;  
  90.         //create an object of datatable  
  91.         dt = new DataTable();  
  92.         //creating column of datatable with datatype  
  93.         pName = new DataColumn("Product_Name", Type.GetType("System.String"));  
  94.         pQty = new DataColumn("Quantity", Type.GetType("System.Int32"));  
  95.         pPrice = new DataColumn("Price", Type.GetType("System.Int32"));  
  96.         //bind data table columns in datatable  
  97.         dt.Columns.Add(pName);  
  98.         dt.Columns.Add(pQty);  
  99.         dt.Columns.Add(pPrice);  
  100.         //creating data row and assiging the value to columns of datatable  
  101.         dr = dt.NewRow();  
  102.         dr["Product_Name"] = "Product 1";  
  103.         dr["Quantity"] = 2;  
  104.         dr["Price"] = 200;  
  105.         dt.Rows.Add(dr);  
  106.   
  107.         dr = dt.NewRow();  
  108.         dr["Product_Name"] = "Product 2";  
  109.         dr["Quantity"] = 5;  
  110.         dr["Price"] = 480;  
  111.         dt.Rows.Add(dr);  
  112.   
  113.         dr = dt.NewRow();  
  114.         dr["Product_Name"] = "Product 3";  
  115.         dr["Quantity"] = 8;  
  116.         dr["Price"] = 100;  
  117.         dt.Rows.Add(dr);  
  118.   
  119.         dr = dt.NewRow();  
  120.         dr["Product_Name"] = "Product 4";  
  121.         dr["Quantity"] = 2;  
  122.         dr["Price"] = 500;  
  123.         dt.Rows.Add(dr);  
  124.         //Add datatable to the dataset  
  125.         ds.Tables.Add(dt);  
  126.         //bind data to data controls  
  127.         FormView1.DataSource = ds.Tables[0];  
  128.         FormView1.DataBind();  
  129.     }  
  130.     //paging code  
  131.     protected void FormView1_PageIndexChanging(object sender, FormViewPageEventArgs e)   
  132.     {  
  133.         FormView1.PageIndex = e.NewPageIndex;  
  134.         BindFormView();  
  135.     }  
  136.   
  137.     protected void BindDetailsView()   
  138.     {  
  139.         //Creating a dataset  
  140.         DataSet ds = new DataSet();  
  141.         DataTable dt;  
  142.         DataRow dr;  
  143.         DataColumn pName;  
  144.         DataColumn pQty;  
  145.         DataColumn pPrice;  
  146.         //create an object of datatable  
  147.         dt = new DataTable();  
  148.         //creating column of datatable with datatype  
  149.         pName = new DataColumn("Product_Name", Type.GetType("System.String"));  
  150.         pQty = new DataColumn("Quantity", Type.GetType("System.Int32"));  
  151.         pPrice = new DataColumn("Price", Type.GetType("System.Int32"));  
  152.         //bind data table columns in datatable  
  153.         dt.Columns.Add(pName);  
  154.         dt.Columns.Add(pQty);  
  155.         dt.Columns.Add(pPrice);  
  156.         //creating data row and assiging the value to columns of datatable  
  157.         dr = dt.NewRow();  
  158.         dr["Product_Name"] = "Product 1";  
  159.         dr["Quantity"] = 2;  
  160.         dr["Price"] = 200;  
  161.         dt.Rows.Add(dr);  
  162.   
  163.         dr = dt.NewRow();  
  164.         dr["Product_Name"] = "Product 2";  
  165.         dr["Quantity"] = 5;  
  166.         dr["Price"] = 480;  
  167.         dt.Rows.Add(dr);  
  168.   
  169.         dr = dt.NewRow();  
  170.         dr["Product_Name"] = "Product 3";  
  171.         dr["Quantity"] = 8;  
  172.         dr["Price"] = 100;  
  173.         dt.Rows.Add(dr);  
  174.   
  175.         dr = dt.NewRow();  
  176.         dr["Product_Name"] = "Product 4";  
  177.         dr["Quantity"] = 2;  
  178.         dr["Price"] = 500;  
  179.         dt.Rows.Add(dr);  
  180.         //Add datatable to the dataset  
  181.         ds.Tables.Add(dt);  
  182.         //bind data to data controls  
  183.         DetailsView1.DataSource = ds.Tables[0];  
  184.         DetailsView1.DataBind();  
  185.     }  
  186.     //paging code  
  187.     protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e)  
  188.     {  
  189.         DetailsView1.PageIndex = e.NewPageIndex;  
  190.         BindDetailsView();  
  191.     }  
  192.   
  193.     protected void BindGridView()   
  194.     {  
  195.         //Creating a dataset  
  196.         DataSet ds = new DataSet();  
  197.         DataTable dt;  
  198.         DataRow dr;  
  199.         DataColumn pName;  
  200.         DataColumn pQty;  
  201.         DataColumn pPrice;  
  202.         //create an object of datatable  
  203.         dt = new DataTable();  
  204.         //creating column of datatable with datatype  
  205.         pName = new DataColumn("Product_Name", Type.GetType("System.String"));  
  206.         pQty = new DataColumn("Quantity", Type.GetType("System.Int32"));  
  207.         pPrice = new DataColumn("Price", Type.GetType("System.Int32"));  
  208.         //bind data table columns in datatable  
  209.         dt.Columns.Add(pName);  
  210.         dt.Columns.Add(pQty);  
  211.         dt.Columns.Add(pPrice);  
  212.         //creating data row and assiging the value to columns of datatable  
  213.         dr = dt.NewRow();  
  214.         dr["Product_Name"] = "Product 1";  
  215.         dr["Quantity"] = 2;  
  216.         dr["Price"] = 200;  
  217.         dt.Rows.Add(dr);  
  218.   
  219.         dr = dt.NewRow();  
  220.         dr["Product_Name"] = "Product 2";  
  221.         dr["Quantity"] = 5;  
  222.         dr["Price"] = 480;  
  223.         dt.Rows.Add(dr);  
  224.   
  225.         dr = dt.NewRow();  
  226.         dr["Product_Name"] = "Product 3";  
  227.         dr["Quantity"] = 8;  
  228.         dr["Price"] = 100;  
  229.         dt.Rows.Add(dr);  
  230.   
  231.         dr = dt.NewRow();  
  232.         dr["Product_Name"] = "Product 4";  
  233.         dr["Quantity"] = 2;  
  234.         dr["Price"] = 500;  
  235.         dt.Rows.Add(dr);  
  236.         //Add datatable to the dataset  
  237.         ds.Tables.Add(dt);  
  238.         //bind data to data controls  
  239.         gvExample.DataSource = ds.Tables[0];  
  240.         gvExample.DataBind();  
  241.   
  242.     }  
  243.     //paging code  
  244.     protected void gvExample_PageIndexChanging(object sender, GridViewPageEventArgs e)   
  245.     {  
  246.         gvExample.PageIndex = e.NewPageIndex;  
  247.         BindGridView();  
  248.     }  
  249. }  

Output

Figure 1:Output

I hope you liked this. Have a good day. Thank you for reading. 


Similar Articles