|
Author: Jenny Nguyen
|
|
Often you will get an error “The GridView 'GridDataBound' fired event PageIndexChanging which wasn't handled” when you set the property AllowPaging of the gridview to true. If you are using the SqlDataSource or OjbectDataSource then you will not get an error however if you are binding the data directly from the dataset then you will get the PageIndexChanging error. To remedy this error you will need to implement the PageIndexChanging event. ASPX Page: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="DGridview.aspx.cs" Inherits="_DGridview" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>GridView Demo</title> </head> <body> <form id="form1" runat="server"> <div> <h1>GridView Demo</h1>
<asp:GridView ID="GridDataBound" runat="server" AutoGenerateColumns="False" AllowPaging="true" PageSize="2"> <Columns> <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" /> <asp:BoundField DataField="Age" HeaderText="Age" SortExpression="Age" /> <asp:BoundField DataField="MarksInMaths" HeaderText="MarksInMaths" SortExpression="MarksInMaths" /> <asp:BoundField DataField="MarksInEnglish" HeaderText="MarksInEnglish" SortExpression="MarksInEnglish" /> <asp:BoundField DataField="MarksInScience" HeaderText="MarksInScience" SortExpression="MarksInScience" /> <asp:BoundField DataField="TotalMarks" HeaderText="TotalMarks" SortExpression="TotalMarks" /> <asp:BoundField DataField="ObtainedMarks" HeaderText="ObtainedMarks" SortExpression="ObtainedMarks" /> </Columns> </asp:GridView>
</div> <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetStudents" TypeName="QVISLib.SampleData"> </asp:ObjectDataSource> </form> </body> </html> Class File: using System; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq;
public partial class _DGridview : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //Create a new databound event GridDataBound.DataBound += new EventHandler(GridDataBound_DataBound); GridDataBound.RowDataBound += new GridViewRowEventHandler(GridDataBound_RowDataBound); GridDataBound.PageIndexChanging += new GridViewPageEventHandler(GridDataBound_PageIndexChanging);
LoadData(); }
void GridDataBound_RowDataBound(object sender, GridViewRowEventArgs e) { //throw new NotImplementedException(); }
void GridDataBound_DataBound(object sender, EventArgs e) { GridDataBound.ShowFooter = true;
//Remove un-use GridDataBound for (int i = GridDataBound.Columns.Count - 1; i > 0; i--) { GridDataBound.FooterRow.Cells.RemoveAt(i); }
GridDataBound.FooterRow.Cells[0].ColumnSpan = GridDataBound.Columns.Count; GridDataBound.FooterRow.Cells[0].HorizontalAlign = HorizontalAlign.Center;
GridDataBound.FooterRow.Cells[0].Text = "This is an example of data bound event"; }
/// <summary> /// Event method to allow the page refresh when user clicks on the page number navigation. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void GridDataBound_PageIndexChanging(Object sender, GridViewPageEventArgs e) { GridDataBound.PageIndex = e.NewPageIndex; LoadData(); //Make you have the databind rebind in here other wise the page will not be refreshed. }
public void LoadData() { QVISLib.SampleData data = new QVISLib.SampleData(); GridDataBound.DataSource = data.GetStudents(); GridDataBound.DataBind(); } }
|