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. </div>
  6. lt;
  7. /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. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title></title>
  6. </head>
  7. <body>
  8. <form id="form1" runat="server">
  9. <div>
  10. <asp:GridView ID="GridView1" AllowPaging="true" PageSize="5" OnPageIndexChanging="GridView1_PageIndexChanging"
  11. runat="server"></asp:GridView>
  12. </div>
  13. </form>
  14. </body>
  15. </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. <connectionStrings>
  16. <add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Integrated Security=true;Initial Catalog=ProductDb;" providerName="System.Data.SqlClient"/>
  17. </connectionStrings>
  18. <appSettings>
  19. <add key="MyServices.MyService" value="http://localhost/MySampleService/MyService.asmx"/>
  20. </appSettings>
  21. </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. public partial class DatagridviewList : System.Web.UI.Page
  8. {
  9. protected void Page_Load(object sender, EventArgs e)
  10. {
  11. if (!IsPostBack)
  12. BindGridList();
  13. }
  14. protected List<Emp> GetEmpList()
  15. {
  16. List<Emp> lEmp = new List<Emp>();
  17. Emp oemp = new Emp(1234, "Upendra", "Noida");
  18. lEmp.Add(oemp);
  19. oemp = new Emp(1234, "Upendra", "Noida");
  20. lEmp.Add(oemp);
  21. oemp = new Emp(1294, "Manish", "Noida");
  22. lEmp.Add(oemp);
  23. oemp = new Emp(1245, "Gitendra", "Delhi");
  24. lEmp.Add(oemp);
  25. oemp = new Emp(1734, "Upendra", "Noida");
  26. lEmp.Add(oemp);
  27. oemp = new Emp(1224, "Amit", "Delhi");
  28. lEmp.Add(oemp);
  29. oemp = new Emp(1204, "Jacob", "Noida");
  30. lEmp.Add(oemp);
  31. oemp = new Emp(1374, "Vishal", "Noida");
  32. lEmp.Add(oemp);
  33. oemp = new Emp(1934, "Rahul", "Noida");
  34. lEmp.Add(oemp);
  35. return lEmp;
  36. }
  37. protected void BindGridList()
  38. {
  39. GridView1.DataSource = GetEmpList();
  40. GridView1.DataBind();
  41. }
  42. protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
  43. {
  44. GridView1.PageIndex = e.NewPageIndex;
  45. BindGridList();
  46. }
  47. }
  48. public class Emp
  49. {
  50. public int ID { get; set; }
  51. public string Name { get; set; }
  52. public string City { get; set; }
  53. public Emp(int id, string name, string city)
  54. {
  55. this.ID = id;
  56. this.Name = name;
  57. this.City = city;
  58. }
  59. }
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. }
Output

result

Figure 2

output
Figure 3

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