• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 343
  • Last Modified:

Datagrid wont page

I can't get this to page through results.  Tried to look online, but couldn't find a decent resource.

aspx snippit:

...
<asp:DataGrid align="center" id="DataGrid1" runat="server" PageSize="50" AutoGenerateColumns="False" AllowPaging="True" AllowSorting="True" GridLines="Vertical" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" BackColor="White" CellPadding="3" ForeColor="Black">
               <SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#000099"></SelectedItemStyle>
               <EditItemStyle Font-Size="X-Small"></EditItemStyle>
               <AlternatingItemStyle BackColor="#CCCCCC"></AlternatingItemStyle>
               <ItemStyle Font-Size="Smaller" HorizontalAlign="Center" VerticalAlign="Top"></ItemStyle>
               <HeaderStyle Font-Bold="True" HorizontalAlign="Center" ForeColor="White" BackColor="Black"></HeaderStyle>
               <FooterStyle BackColor="#CCCCCC"></FooterStyle>
               <Columns>
                    <asp:TemplateColumn HeaderText="Event Name">
                         <ItemTemplate>
                              <a href = 'view.aspx?t=<%# DataBinder.Eval(Container, "DataItem.EventID") %>'>
                                   <%# DataBinder.Eval(Container, "DataItem.EventName") %>
                              </a>
                         </ItemTemplate>
                    </asp:TemplateColumn>
                    <asp:BoundColumn DataField="EventCity" HeaderText="City"></asp:BoundColumn>
                    <asp:BoundColumn DataField="EndDate" HeaderText="Event Date*" DataFormatString="{0:MMMM dd, yyyy}"></asp:BoundColumn>
                    <asp:BoundColumn DataField="PlayersTot" HeaderText="Total Players"></asp:BoundColumn>
                    <asp:BoundColumn DataField="GamesTot" HeaderText="Games Total"></asp:BoundColumn>
               </Columns>
               <PagerStyle NextPageText="next page" Font-Size="X-Small" PrevPageText="previous page" HorizontalAlign="Left" ForeColor="Black" Position="TopAndBottom" BackColor="White" Mode="NumericPages"></PagerStyle>
          </asp:DataGrid>
...

codebehind:

private void Page_Load(object sender, System.EventArgs e)
          {
               bokfoowe.getPData mysrv = new bokfoowe.getPData();
               DataGrid1.DataSource = mysrv.GetData(@"SELECT EventID, EventName, EventCity, EndDate, PlayersTot, GamesTot FROM chsTournament ORDER BY enddate DESC","zzzz","zzzz");
               
               DataGrid1.DataBind();
          }



0
thunderchicken
Asked:
thunderchicken
1 Solution
 
DJDougCommented:
You have to code for the OnPageIndexChanged event.  There is a good example at the bottom of the page here:

http://samples.gotdotnet.com/QuickStart/aspplus/default.aspx?url=/quickstart/aspplus/samples/webforms/ctrlref/webctrl/datagrid/doc_datagrid.aspx

Hope that helps.
0
 
Michel SakrCommented:
indeed..


Use the OnPageIndexChanged method to provide a custom handler for the PageIndexChanged event.

The PageIndexChanged event is raised when one of the page selection elements is clicked.

A typical handler for the PageIndexChanged event sets the CurrentPageIndex property to the index of the page you want to display and then uses the DataBind method to bind the data to the DataGrid control.

Note   The event handler receives a DataGridPageChangedEventArgs object as a parameter. You can use the NewPageIndex property of this parameter to determine the index of the page selected by the user from the page selection elements of the DataGrid control.
Raising an event invokes the event handler through a delegate. For more information, see Raising an Event.

Notes to Inheritors:  When overriding OnPageIndexChanged in a derived class, be sure to call the base class's OnPageIndexChanged method.

For more information about handling events, see Consuming Events.

Example
[Visual Basic, C#, JScript] The following example demonstrates how to specify and code a handler for the PageIndexChanged event. It displays pages on the DataGrid control in 10-item increments.

[Visual Basic]
<%@ Import Namespace="System.Data" %>
 
<html>
<script language="VB" runat="server">
 
    Dim Cart As DataTable
    Dim CartView As DataView
    Function CreateDataSource() As ICollection
        Dim dt As New DataTable()
        Dim dr As DataRow
       
        dt.Columns.Add(New DataColumn("IntegerValue", GetType(Int32)))
        dt.Columns.Add(New DataColumn("StringValue", GetType(String)))
        dt.Columns.Add(New DataColumn("CurrencyValue", GetType(Double)))
       
        Dim i As Integer
        For i = 0 To 99
            dr = dt.NewRow()
           
            dr(0) = i
            dr(1) = "Item " & i.ToString()
            dr(2) = 1.23 *(i + 1)
            dt.Rows.Add(dr)
        Next i
       
        Dim dv As New DataView(dt)
        Return dv
    End Function 'CreateDataSource


    Sub Page_Load(sender As Object, e As EventArgs)
       
        If Not IsPostBack Then
            ' Need to load this data only once.
            ItemsGrid.DataSource = CreateDataSource()
            ItemsGrid.DataBind()
        End If
       
        If CheckBox1.Checked Then
            ItemsGrid.PagerStyle.Mode = PagerMode.NumericPages
        Else
            ItemsGrid.PagerStyle.Mode = PagerMode.NextPrev
        End If
    End Sub 'Page_Load


    Sub Grid_Change(sender As Object, e As DataGridPageChangedEventArgs)
       
        ' Set CurrentPageIndex to the page the user clicked.
        ItemsGrid.CurrentPageIndex = e.NewPageIndex
       
        ' Rebind the data.
        ItemsGrid.DataSource = CreateDataSource()
        ItemsGrid.DataBind()
    End Sub 'Grid_Change
</script>
 
<body>
 
   <form runat=server>

   <h3>DataGrid Paging Example</h3>
 
   <asp:DataGrid id="ItemsGrid" runat="server"
        BorderColor="black"
        BorderWidth="1"
        CellPadding="3"
        AllowPaging="true"
        AutoGenerateColumns="false"        
        OnPageIndexChanged="Grid_Change">
 
      <HeaderStyle BackColor="#00aaaa">
      </HeaderStyle>
 
      <PagerStyle Mode="NextPrev">
      </PagerStyle>

      <Columns>

         <asp:BoundColumn
              HeaderText="Number"
              DataField="IntegerValue"/>
         <asp:BoundColumn
              HeaderText="Item"
              DataField="StringValue"/>
         <asp:BoundColumn
              HeaderText="Price"
              DataField="CurrencyValue"
              DataFormatString="{0:c}">
 
            <ItemStyle HorizontalAlign="right">
            </ItemStyle>
     
         </asp:BoundColumn>

      </Columns>

   </asp:DataGrid>

   <br>

   <asp:CheckBox id="CheckBox1"
                 Text="Show page navigation"
                 AutoPostBack="true"
                 runat="server"/>
 
   </form>
 
</body>
</html>
[C#]
<%@ Import Namespace="System.Data" %>
 
<html>
<script language="C#" runat="server">
 
   DataTable Cart;
   DataView CartView;
 
   ICollection CreateDataSource()
   {
      DataTable dt = new DataTable();
      DataRow dr;
 
      dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
      dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
      dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));
 
      for (int i = 0; i < 100; i++)
      {
         dr = dt.NewRow();
 
         dr[0] = i;
         dr[1] = "Item " + i.ToString();
         dr[2] = 1.23 * (i+1);
         dt.Rows.Add(dr);
      }
 
      DataView dv = new DataView(dt);
      return dv;
   }
 
   void Page_Load(Object sender, EventArgs e)
   {
 
      if (!IsPostBack)
      {
         // Need to load this data only once.
         ItemsGrid.DataSource = CreateDataSource();
         ItemsGrid.DataBind();
      }
 
      if (CheckBox1.Checked)
         ItemsGrid.PagerStyle.Mode = PagerMode.NumericPages;
      else
         ItemsGrid.PagerStyle.Mode = PagerMode.NextPrev;

   }
 
   void Grid_Change(Object sender, DataGridPageChangedEventArgs e)
   {
 
      // Set CurrentPageIndex to the page the user clicked.
      ItemsGrid.CurrentPageIndex = e.NewPageIndex;

      // Rebind the data.
      ItemsGrid.DataSource = CreateDataSource();
      ItemsGrid.DataBind();
     
   }
 
 
</script>
 
<body>
 
   <form runat=server>

   <h3>DataGrid Paging Example</h3>
 
   <asp:DataGrid id="ItemsGrid" runat="server"
        BorderColor="black"
        BorderWidth="1"
        CellPadding="3"
        AllowPaging="true"
        AutoGenerateColumns="false"        
        OnPageIndexChanged="Grid_Change">
 
      <HeaderStyle BackColor="#00aaaa">
      </HeaderStyle>
 
      <PagerStyle Mode="NextPrev">
      </PagerStyle>

      <Columns>

         <asp:BoundColumn
              HeaderText="Number"
              DataField="IntegerValue"/>
         <asp:BoundColumn
              HeaderText="Item"
              DataField="StringValue"/>
         <asp:BoundColumn
              HeaderText="Price"
              DataField="CurrencyValue"
              DataFormatString="{0:c}">
 
            <ItemStyle HorizontalAlign="right">
            </ItemStyle>
     
         </asp:BoundColumn>

      </Columns>

   </asp:DataGrid>

   <br>

   <asp:CheckBox id="CheckBox1"
                 Text="Show page navigation"
                 AutoPostBack="true"
                 runat="server"/>
 
   </form>
 
</body>
</html>
[JScript]
<%@ Import Namespace="System.Data" %>
 
<html>
<script language="JScript" runat="server">
 
   var Cart : DataTable = new DataTable();
   var CartView : DataView;  
 
   function CreateDataSource() : ICollection
   {
      var dt : DataTable = new DataTable();
      var dr : DataRow;
 
      dt.Columns.Add(new DataColumn("IntegerValue", Int32));
      dt.Columns.Add(new DataColumn("StringValue", System.String));
      dt.Columns.Add(new DataColumn("CurrencyValue", double));
 
      for (var i : int = 0; i < 100; i++)
      {
         dr = dt.NewRow();
 
         dr[0] = i;
         dr[1] = "Item " + i.ToString();
         dr[2] = 1.23 * (i+1);
         dt.Rows.Add(dr);
      }
 
      var dv : DataView = new DataView(dt);
      return dv;
   }
 
   function Page_Load(sender, e : EventArgs)
   {
 
      if (!IsPostBack)
      {
         // Need to load this data only once.
         ItemsGrid.DataSource = CreateDataSource();
         ItemsGrid.DataBind();
      }
 
      if (CheckBox1.Checked)
         ItemsGrid.PagerStyle.Mode = PagerMode.NumericPages;
      else
         ItemsGrid.PagerStyle.Mode = PagerMode.NextPrev;

   }
 
   function Grid_Change(sender, e : DataGridPageChangedEventArgs)
   {
 
      // Set CurrentPageIndex to the page the user clicked.
      ItemsGrid.CurrentPageIndex = e.NewPageIndex;

      // Rebind the data.
      ItemsGrid.DataSource = CreateDataSource();
      ItemsGrid.DataBind();
     
   }
 
 
</script>
 
<body>
 
   <form runat=server>

   <h3>DataGrid Paging Example</h3>
 
   <asp:DataGrid id="ItemsGrid" runat="server"
        BorderColor="black"
        BorderWidth="1"
        CellPadding="3"
        AllowPaging="true"
        AutoGenerateColumns="false"        
        OnPageIndexChanged="Grid_Change">
 
      <HeaderStyle BackColor="#00aaaa">
      </HeaderStyle>
 
      <PagerStyle Mode="NextPrev">
      </PagerStyle>

      <Columns>

         <asp:BoundColumn
              HeaderText="Number"
              DataField="IntegerValue"/>
         <asp:BoundColumn
              HeaderText="Item"
              DataField="StringValue"/>
         <asp:BoundColumn
              HeaderText="Price"
              DataField="CurrencyValue"
              DataFormatString="{0:c}">
 
            <ItemStyle HorizontalAlign="right">
            </ItemStyle>
     
         </asp:BoundColumn>

      </Columns>

   </asp:DataGrid>

   <br>

   <asp:CheckBox id="CheckBox1"
                 Text="Show page navigation"
                 AutoPostBack="true"
                 runat="server"/>
 
   </form>
 
</body>
</html>
[C++] No example is available for C++. To view a Visual Basic, C#, or JScript example, click the Language Filter button  in the upper-left corner of the page.

Requirements
0
 
SeanGraflundCommented:
in html design view, click on the datagrid, then click on properties .. then click on the lightning bolt (event) button .. then couble click "page index changed" event ..

put this in page index changed:

DataGrid1.CurrentPageIndex = e.NewPageIndex;
              bokfoowe.getPData mysrv = new bokfoowe.getPData();
              DataGrid1.DataSource = mysrv.GetData(@"SELECT EventID, EventName, EventCity, EndDate, PlayersTot, GamesTot FROM chsTournament ORDER BY enddate DESC","zzzz","zzzz");
             
              DataGrid1.DataBind();
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now