.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
satyapriyanayak
Subhransu Sekhar Jena
abhays
SP
DotnetUser

  

How to perform implement in a GridView in ASP.Net

   
 
     


Introduction :

In this tutorial we will learn how to perform or implement simple Pagination in a gridview with C# and VB.Net code example step by step.

To perform paging in a gridview we need to minimum set it 2 property and handle one event.

All you need is to set Griview Pagesize Property to any integer value let's say 10 and AllowPaging = true.

Also, you need to handle onpageindexchanging  of GridView in your code file .cs or .vb page

So your GridView html code looks like this. In this my Gridview id is GridView1

<asp:GridView ID="GridView1" runat="server" PageSize="10" AllowPaging="true"
  onpageindexchanging="GridView1_PageIndexChanging" >
 
</asp:GridView>

In below code I am creating a DataTable and bind it to my Gridview so that we can see some data in our gridview

C# Code sample

private DataTable GetDataTable()
{

    // Create an object of DataTable class
    DataTable dataTable = new DataTable("MyDataTable");

    //Create ID DataColumn
    DataColumn dataColumn_ID = new DataColumn("ID", typeof(Int32));

    dataTable.Columns.Add(dataColumn_ID);

    //Create another DataColumn Name
    DataColumn dataColumn_Name = new DataColumn("Name", typeof(string));

    dataTable.Columns.Add(dataColumn_Name);

    //Now Add some row to newly created dataTable
    DataRow dataRow;

    for (int i = 0; i < 25; i++)
    {

        dataRow = dataTable.NewRow();// Important you have create New row

        dataRow["ID"] = i;

        dataRow["Name"] = "Name_" + i.ToString();

        dataTable.Rows.Add(dataRow);

    }

    dataTable.AcceptChanges();

    return dataTable;

}

VB.NET sample code

Private Function GetDataTable() As DataTable

	' Create an object of DataTable class
	Dim dataTable As New DataTable("MyDataTable")

	'Create ID DataColumn
	Dim dataColumn_ID As New DataColumn("ID", GetType(Int32))

	dataTable.Columns.Add(dataColumn_ID)

	'Create another DataColumn Name
	Dim dataColumn_Name As New DataColumn("Name", GetType(String))

	dataTable.Columns.Add(dataColumn_Name)

	'Now Add some row to newly created dataTable
	Dim dataRow As DataRow

	For i As Integer = 0 To 24

		dataRow = dataTable.NewRow()
		' Important you have create New row
		dataRow("ID") = i

		dataRow("Name") = "Name_" & i.ToString()


		dataTable.Rows.Add(dataRow)
	Next

	dataTable.AcceptChanges()

	Return dataTable

End Function


Now I am binding the above created DataTable to my Gridview in Button click event

C# Code

protected void Button1_Click(object sender, EventArgs e)
{
    GridView1.DataSource = GetDataTable();
    GridView1.DataBind();
}

VB.Net Code

Protected Sub Button1_Click(sender As Object, e As EventArgs)
	GridView1.DataSource = GetDataTable()
	GridView1.DataBind()
End Sub


Handle onpageindexchanging Event.


If you don't handle above event it will throw this error.


The GridView 'GridView1' fired event PageIndexChanging which wasn't handled.

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;

    // Bind the gridview again
    GridView1.DataSource = GetDataTable();
    GridView1.DataBind();

}

VB.NET Code example

Protected Sub GridView1_PageIndexChanging(sender As Object, e As GridViewPageEventArgs)
	GridView1.PageIndex = e.NewPageIndex

	' Bind the gridview again
	GridView1.DataSource = GetDataTable()
	GridView1.DataBind()

End Sub


That's it..you are done..Now run your asp.net website or application and you will see below output

Other Tutorials in the same Category:
      
  • How to set the number of rows to be displayed in a GridView in ASP.Net?      
  • How to perform implement in a GridView in ASP.Net      
  • Add html controls inside gridview in asp.net      
  • Add header text to gridview templatefield      
  • Change BackGround color of a row of Gridview based on condition in asp.net      
  • Set the fixed width of a column of the GridView      
  • How to hide or remove GridView headers?      
  • Change Cell Background Color of Gridview dynamically

  •  
         
     
    Return to Main Index Page


    Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend