?
Solved

Datagrid wont page

Posted on 2003-02-28
3
Medium Priority
?
336 Views
Last Modified: 2012-06-22
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
Comment
Question by:thunderchicken
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 4

Accepted Solution

by:
DJDoug earned 800 total points
ID: 8041696
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
 
LVL 20

Expert Comment

by:Silvers5
ID: 8042034
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
 
LVL 1

Expert Comment

by:SeanGraflund
ID: 8042125
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question