Como usar “sorting” y “paging” en un gridview

By | 2010/12/21

Si tratas de usar sorting y paging a la vez en un gridview el sistema de devolverá un error como este:

“The GridView ‘GridViewID’ fired event PageIndexChanging which wasn’t handled.”

Lo que se debe hacer en el gridview es agregar los llamados que manejarán los eventos:

<asp:GridView ID=”gridView” OnPageIndexChanging=”gridView_PageIndexChanging” OnSorting=”gridView_Sorting” runat=”server” />

Y en el código las funciones:

private string ConvertSortDirectionToSql(SortDirection sortDirection)
{
string newSortDirection = String.Empty;

switch (sortDirection)
{
case SortDirection.Ascending:
newSortDirection = “ASC”;
break;

case SortDirection.Descending:
newSortDirection = “DESC”;
break;
}

return newSortDirection;
}

protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gridView.PageIndex = e.NewPageIndex;
gridView.DataBind();
}

protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dataTable = gridView.DataSource as DataTable;

if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
dataView.Sort = e.SortExpression + ” ” + ConvertSortDirectionToSql(e.SortDirection);

gridView.DataSource = dataView;
gridView.DataBind();
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This blog is kept spam free by WP-SpamFree.