aspx Tutorial

NET Articles,jQuery demo, asp.net with jQuery, online tutorial,Jquery, SilverLight, Javascript, asp.net,JSON, MVC,.NET Articles,demo, Web Services,
Advertise Here

Toolbar

Get our toolbar!

Advertize



Posted by Aamir Hasan   on Wednesday, November 24, 2010 Total Views:  

 

GridView is one of most widely used control and we can do lots of things with this control. In ASP.Net 2.0, DataGrid is replaced by GridView control while DataGrid control is still supported. There are large numbers of tasks that can be done through GridView control.

Here’s is the solution.


     <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="false"
               PageSize="5" OnPageIndexChanging="GridView1_PageIndexChanging">
               <Columns>
                   <asp:TemplateField>
                       <HeaderTemplate>
                           Sr.#
                       </HeaderTemplate>
                       <ItemTemplate>
                           <%# Container.DataItemIndex + 1 %>
                       </ItemTemplate>
                   </asp:TemplateField>
                   <asp:BoundField DataField="FullName" HeaderText="Full Name" />
                   <asp:BoundField DataField="Age" HeaderText="Age" />
               </Columns>
       </asp:GridView>

 

Code Behind

   protected void Page_Load(object sender, EventArgs e)
   {
       GridView1.DataSource = fetchAllEmployee().ToList<Employee>();
       GridView1.DataBind();

   }
   protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
   {
       GridView1.PageIndex = e.NewPageIndex;
       GridView1.DataBind();
   }
   public List<Employee> fetchAllEmployee()
   {
       return new List<Employee>()
       {
           new Employee{ Fullname="Aamir Hasan",age=26},
           new Employee{ Fullname="Awais Ahmed",age=18},
           new Employee{ Fullname="Mahwish khan",age=24},
           new Employee{ Fullname="Gill",age=55},
           new Employee{ Fullname="Saba khan",age=19},
           new Employee{ Fullname="adnan",age=26},
           new Employee{ Fullname="fasial hameed",age=20},
           new Employee{ Fullname="Aamir Hasan",age=16},
           new Employee{ Fullname="John",age=45},
           new Employee{ Fullname="Suprotim Agarwal ",age=34},
           new Employee{ Fullname="sana",age=24}
       };
   }

   public class Employee
   {
       public String Fullname { get; set; }
       public int age { get; set; }
   }

 

VB.NET

   Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
       GridView1.DataSource = fetchAllEmployee()
       GridView1.DataBind()
   End Sub
   Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
       GridView1.PageIndex = e.NewPageIndex
       GridView1.DataBind()
   End Sub
   Public Function fetchAllEmployee() As List(Of Employee)
       Dim emps As New List(Of Employee)()
       emps.Add(New Employee() With { _
        .Fullname = "Aamir Hasan", _
        .age = 26 _
       })
       emps.Add(New Employee() With { _
        .Fullname = "Awais Ahmed", _
        .age = 18 _
       })
       emps.Add(New Employee() With { _
        .Fullname = "Mahwish khan", _
        .age = 24 _
       })
       emps.Add(New Employee() With { _
        .Fullname = "Gill", _
        .age = 55 _
       })
       emps.Add(New Employee() With { _
        .Fullname = "Saba khan", _
        .age = 19 _
       })
       emps.Add(New Employee() With { _
        .Fullname = "adnan", _
        .age = 26 _
       })
       emps.Add(New Employee() With { _
        .Fullname = "fasial hameed", _
        .age = 20 _
       })
       emps.Add(New Employee() With { _
        .Fullname = "Aamir Hasan", _
        .age = 16 _
       })
       emps.Add(New Employee() With { _
        .Fullname = "John", _
        .age = 45 _
       })
       emps.Add(New Employee() With { _
        .Fullname = "Suprotim Agarwal ", _
        .age = 34 _
       })
       emps.Add(New Employee() With { _
        .Fullname = "sana", _
        .age = 24 _
       })

       Return emps
   End Function

   Public Class Employee
       Public Property Fullname() As [String]
           Get
               Return m_Fullname
           End Get
           Set(ByVal value As [String])
               m_Fullname = Value
           End Set
       End Property
       Private m_Fullname As [String]
       Public Property age() As Integer
           Get
               Return m_age
           End Get
           Set(ByVal value As Integer)
               m_age = Value
           End Set
       End Property
       Private m_age As Integer
   End Class

Note:Following error message shown to you when you set AllowPaging="True" in gridview control.“The GridView fired event PageIndexChanging which wasn’t handled error message.”,you all need to add event handler  GridView1_PageIndexChanging  of gridview control which is already added in above code.


Output

Download

Row number in gridview.zip (2.54 kb)

See live demo


Protected by Copyscape Online Plagiarism Tool

Comments (3) -

kamal
kamal Vietnam
1/4/2013 7:32:28 AM #

kamal

datanesia.com
datanesia.com Indonesia
2/27/2013 1:45:38 AM #

thank your information

www.datanesia.com
www.datanesia.com Indonesia
5/24/2013 1:44:55 AM #

good

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Advertizement 1
Advertizement 2
Advertizement 3
Advertizement 4
Advertizement 5