Paging gridview

I have a simple query:

Dim info As New SailMembersDataContext
Dim query = From p In info.Individuals
GridView1.DataSource = query
GridView1.DataBind()

and I was wondering how I can add paging to this request, for example, 10 per page, I tried using the built-in paging in the GridView, but this just causes an error:

In the GridView 'GridView1' an event was fired PageIndexChangingthat was not processed.

When trying to go to another page.

+3
source share
2 answers

Ok it's easy

Handle the PageIndexChangingevent in your code behind,

WITH#

void GridView1_PageIndexChanging(Object sender, GridViewPageEventArgs e)
{
    //For example
    //Cancel the paging operation if the user attempts to navigate
    //to another page while the GridView control is in edit mode. 
    if (GridView1.EditIndex != -1)
    {
        // Use the Cancel property to cancel the paging operation.
        e.Cancel = true;

        // Display an error message.
        int newPageNumber = e.NewPageIndex + 1;
        Message.Text = "Please update the record before moving to page " +
        newPageNumber.ToString() + ".";
    }
    else
    {
        // Clear the error message.
        Message.Text = "";
    }
}

Vb.net

Private Sub GridView1_PageIndexChanging(sender As [Object], e As GridViewPageEventArgs)
    'For example
    'Cancel the paging operation if the user attempts to navigate
    'to another page while the GridView control is in edit mode. 
    If GridView1.EditIndex <> -1 Then
        ' Use the Cancel property to cancel the paging operation.
        e.Cancel = True

        ' Display an error message.
        Dim newPageNumber As Integer = e.NewPageIndex + 1
        Message.Text = "Please update the record before moving to page " & newPageNumber.ToString() & "."
    Else
        ' Clear the error message.
        Message.Text = ""
    End If
End Sub

And your markup will be like this:

<asp:gridview id="GridView1" 
    autogeneratecolumns="true"
    emptydatatext="No data available." 
    allowpaging="true"
    autogenerateeditbutton="true"
    onpageindexchanging="GridView1_PageIndexChanging" 
    runat="server">
    <pagersettings mode="Numeric"
      position="Bottom"           
      pagebuttoncount="10"/>

    <pagerstyle backcolor="LightBlue"/>

  </asp:gridview>
+3
source

You need an event handling method.

/// <summary>
/// Handles the PageIndexChanging event.  
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The instance containing the event data.</param>    
public static void GridViewPageIndexChanging(object sender, GridViewPageEventArgs e)
{
    [your application functionality here]
 }
0
source

Source: https://habr.com/ru/post/1789227/


All Articles