how to display grid view data in first,last,previous,Last in asp.net

Posted by Venky.Net under ASP.NET on 4/8/2014 | Points: 10 | Views : 1853 | Status : [Member] | Replies : 3
hi

i have one gridview and 4 link button First,Next,Previous,Last so i am click first link button i want display gridview first record only and same as remaining three button.




Responses

Posted by: A2H on: 4/8/2014 [Member] [MVP] Silver | Points: 25

Up
0
Down
I guess your requirement is to show paging options like First Next Previous Last in gridview. if that is the case then your best option would be to use the GridView PagerSettings Mode property. you need to set the Pagersettings mode property to "NextPreviousFirstLast".

Please find the sample implementation
<asp:GridView ID="GridView5" runat="server" AutoGenerateColumns="False" DataKeyNames="ID"

CellPadding="4" ForeColor="#333333" GridLines="None" AllowPaging="true" PageSize="1"
OnPageIndexChanging="GridView5_PageIndexChanging">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField HeaderText="ID" DataField="ID" />
<asp:BoundField HeaderText="Item" DataField="Item" />
<asp:BoundField HeaderText="FirstName" DataField="FirstName" />
<asp:BoundField HeaderText="EmployeeSalary" DataField="EmployeeSalary" />
</Columns>
<PagerSettings Mode="NextPreviousFirstLast" FirstPageText="First" PreviousPageText="Previous" Visible="true"
NextPageText="Next" LastPageText="Last" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
</asp:GridView>


C#:
protected void Page_Load(object sender, EventArgs e)

{
if (!IsPostBack)
{
//Assign the data to GridView
GridView5.DataSource = this.Get_TotalDetails();
//Bind the Grid
GridView5.DataBind();

}
}

//Populate some dummy data
public DataTable Get_TotalDetails()
{

DataTable dt = new DataTable();
DataRow dr;
string ID = "70123,28123123,70123123,5212313,4012313";
string Item1 = "Bat,Ball,Stumps,Caps,TeamBand";
string FirstName = "Name1,Name2,Name3,Name4,Name5";
string EmployeeSalary = "7000,28876,3498,5232,40456";
//string SelectedField = "True,False,False,False,True";
string[] list4 = ID.Split(',');
string[] list1 = Item1.Split(',');
string[] list2 = FirstName.Split(',');
string[] list3 = EmployeeSalary.Split(',');
//string[] list3 = SelectedField.Split(',');
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Item", typeof(string));
dt.Columns.Add("FirstName", typeof(string));
dt.Columns.Add("EmployeeSalary", typeof(int));
for (int i = 0; i < list4.Length; i++)
{
dr = dt.NewRow();
dr["ID"] = list4[i];
dr["Item"] = list1[i];
dr["FirstName"] = list2[i];
dr["EmployeeSalary"] = list3[i];
dt.Rows.Add(dr);
}

return dt;
}

/// <summary>
/// Handles the PageIndexChanging event of the GridView5 control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="GridViewPageEventArgs"/> instance containing the event data.</param>
protected void GridView5_PageIndexChanging(object sender, GridViewPageEventArgs e)
{

GridView5.PageIndex = e.NewPageIndex;
//Assign your datasource here.
//If you are having data fetching method in any other method call that method here
GridView5.DataSource = this.Get_TotalDetails();
GridView5.DataBind();
}


You can see the rendered output here
http://i.imgur.com/IvRJNS9.png

Please mark my reply as answer if it helps to resolve your problem


Thanks,
A2H
My Blog

Venky.Net, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: A2H on: 4/8/2014 [Member] [MVP] Silver | Points: 25

Up
0
Down
If you still want to implement the functionality using linkbutton itself then you can check the below link
http://www.aspdotnet-suresh.com/2011/03/gridview-with-custom-paging.html

Please mark my reply as answer if it helps to resolve your problem

Thanks,
A2H
My Blog

Venky.Net, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rimi1289 on: 4/9/2014 [Member] Starter | Points: 25

Up
0
Down
Not exactly what you are looking for, but this (http://www.encodedna.com/2013/06/sum-of-gridview-column-in-footer.htm ) article can give you an idea about how this can be done.

Venky.Net, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response