Binding GridView Using List in ASP.Net

Also demonstrated the each controls paging. We learn here how to bind controls with data without a database and how to handle paging on the controls.

Here you learn how to add a list to a GridView as a Data Source.

Read this also:

Initial chamber

Step 1

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

Step 2

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

For Web Form

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

Design chamber

Step 3

Open the DatagridviewList.aspx file and write some code for the design of the application. This is the design phase.

Here I've design a GridView Control and used a property here:

  1. <form id="form1" runat="server">  
  2.    <div>  
  3.        <asp:GridView ID="GridView1" AllowPaging="true" PageSize="5" OnPageIndexChanging="GridView1_PageIndexChanging"  
  4.             runat="server"></asp:GridView>  
  5.   
  6.    </div>  
  7.    lt;
  8. /form>  
Your design looks as in Figure 1.

Design
Figure 1

Now your design page looks as in the following.

Design Page
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="DatagridviewList.aspx.cs" Inherits="DatagridviewList" %>  
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.         <div>  
  12.             <asp:GridView ID="GridView1" AllowPaging="true" PageSize="5" OnPageIndexChanging="GridView1_PageIndexChanging"  
  13.                  runat="server"></asp:GridView>  
  14.   
  15.         </div>  
  16.     </form>  
  17. </body>  
  18. </html>  
Config Section
  1. <?xml version="1.0"?>  
  2. <!--  
  3.   For more information on how to configure your ASP.NET application, please visit  
  4.   http://go.microsoft.com/fwlink/?LinkId=169433  
  5.   -->  
  6. <configuration>  
  7.   <system.web>  
  8.     <compilation debug="true" targetFramework="4.5">  
  9.       <assemblies>  
  10.         <add assembly="Microsoft.Office.Interop.Word, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71E9BCE111E9429C"/>  
  11.       </assemblies>  
  12.     </compilation>  
  13.     <httpRuntime targetFramework="4.5"/>  
  14.   </system.web>  
  15.     
  16.   <connectionStrings>  
  17.      
  18.     <add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Integrated Security=true;Initial Catalog=ProductDb;" providerName="System.Data.SqlClient"/>  
  19.   </connectionStrings>  
  20.     
  21.   <appSettings>  
  22.     <add key="MyServices.MyService" value="http://localhost/MySampleService/MyService.asmx"/>  
  23.      
  24.       </appSettings>  
  25. </configuration>  
Code chamber

Step 4

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

Adding the following namespaces in the namespace section of your code behind page as in the following:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
Now your page looks as in the following.

Code behind Page

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8. public partial class DatagridviewList : System.Web.UI.Page  
  9. {  
  10.     protected void Page_Load(object sender, EventArgs e)  
  11.     {  
  12.         if (!IsPostBack)  
  13.             BindGridList();  
  14.     }  
  15.     protected List<Emp> GetEmpList()  
  16.     {  
  17.         List<Emp> lEmp = new List<Emp>();  
  18.         Emp oemp = new Emp(1234, "Upendra""Noida");  
  19.         lEmp.Add(oemp);  
  20.         oemp = new Emp(1234, "Upendra""Noida");  
  21.         lEmp.Add(oemp);  
  22.         oemp = new Emp(1294, "Manish""Noida");  
  23.         lEmp.Add(oemp);  
  24.         oemp = new Emp(1245, "Gitendra""Delhi");  
  25.         lEmp.Add(oemp);  
  26.         oemp = new Emp(1734, "Upendra""Noida");  
  27.         lEmp.Add(oemp);  
  28.         oemp = new Emp(1224, "Amit""Delhi");  
  29.         lEmp.Add(oemp);  
  30.         oemp = new Emp(1204, "Jacob""Noida");  
  31.         lEmp.Add(oemp);  
  32.         oemp = new Emp(1374, "Vishal""Noida");  
  33.         lEmp.Add(oemp);  
  34.         oemp = new Emp(1934, "Rahul""Noida");  
  35.         lEmp.Add(oemp);  
  36.         return lEmp;  
  37.     }  
  38.   
  39.     protected void BindGridList()  
  40.     {  
  41.         GridView1.DataSource = GetEmpList();  
  42.         GridView1.DataBind();  
  43.     }  
  44.   
  45.     protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)  
  46.     {  
  47.         GridView1.PageIndex = e.NewPageIndex;  
  48.         BindGridList();  
  49.   
  50.     }  
  51. }  
  52. public class Emp  
  53. {  
  54.     public int ID { getset; }  
  55.     public string Name { getset; }  
  56.     public string City { getset; }  
  57.     public Emp(int id, string name, string city)  
  58.     {  
  59.         this.ID = id;  
  60.         this.Name = name;  
  61.         this.City = city;  
  62.     }  
  63. }  
Here paging code is specified for paging. I've used a GridView paging for that.

Paging Code
  1. protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)  
  2. {  
  3.    GridView1.PageIndex = e.NewPageIndex;  
  4.    BindGridList();  
  5.   
  6. }  
Output

result

Figure 2

output
Figure 3

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


Similar Articles