Solved

Can't get DataGrid to page. in C#

Posted on 2010-08-30
22
1,394 Views
Last Modified: 2012-05-10
hello experts,

 I am having a heck of a time with a datagrid. This datagrid gets populated on a button click with results from a database. I want the datagrid to display the first 10 results and then be able to "page" to the next results and so on...... I can get the datagrid to page but other than that I cant get the darn thing to work for anything....

in the DataGrid1 Propertis page I have allow paging and allow custom paging selected with the page siae = 10. Show navigation buttons is selected as well... what am I doing wrong.
<asp:DataGrid ID="DataGrid1" runat="server" AllowPaging="True" 
        AllowSorting="True" ShowFooter="True" AllowCustomPaging="True">
                <PagerStyle NextPageText=">" PrevPageText="<" BackColor="#FFCC66" 
                    ForeColor="#333333" HorizontalAlign="Center" />
            </asp:DataGrid>

Open in new window

0
Comment
Question by:HawaiiDragon
[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
  • 11
  • 10
22 Comments
 
LVL 30

Expert Comment

by:MlandaT
ID: 33559269
Private Sub GridPageIndexChanged(source As Object, e As System.Web.UI.WebControls.DataGridPageChangedEventArgs)
      DataGrid1.CurrentPageIndex = e.NewPageIndex
      'Bind the DataGrid again with the Data Source
      DataGrid1.DataSource = dataSet1
      DataGrid1.DataBind()
End Sub

and in the ASPX:


        AllowSorting="True" ShowFooter="True" AllowCustomPaging="True" OnPageIndexChanged="GridPageIndexChanged">
0
 

Author Comment

by:HawaiiDragon
ID: 33559428
how would I do this in C# I dont know VB
0
 
LVL 30

Expert Comment

by:MlandaT
ID: 33559488
private void GridPageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
      DataGrid1.CurrentPageIndex = e.NewPageIndex;
      //Bind the DataGrid again with the Data Source
      DataGrid1.DataSource = dataSet1;
      DataGrid1.DataBind();
}
0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 5

Expert Comment

by:Akin Delu
ID: 33559493
In C#

private void GridPageIndexChanged(object sender, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
     DataGrid1.CurrentPageIndex = e.NewPageIndex;
     //Bind the DataGrid again with the Data Source
     DataGrid1.DataSource = dataSet1;
     DataGrid1.DataBind();
}
0
 

Author Comment

by:HawaiiDragon
ID: 33559494
this is how I have it working... I tried to put your solustion in and it does not llike it.... and its in a public function.... I cant figure out why it wont use it.

 public void work()
    {
        System.Data.SqlClient.SqlConnection con4 = new System.Data.SqlClient.SqlConnection();
        con4.ConnectionString = ConfigurationManager.ConnectionStrings["strDBCourseStandard"].ConnectionString;
        con4.Open();
        string strSQL = "Select distinct * from schedule Where SchoolYear = 2008";
        SqlCommand objCommand = new SqlCommand(strSQL, con4);
        SqlDataReader objReader = objCommand.ExecuteReader();
        DataGrid1.DataSource = objReader;
        DataGrid1.DataBind();
        objReader.Close();
      
    }

protected void Button1_Click(object sender, EventArgs e)
    {
        smash();
        work();
        working2();
    }

private void GridPageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
    {
        DataGrid1.CurrentPageIndex = e.NewPageIndex;
        //Bind the DataGrid again with the Data Source
        DataGrid1.DataSource = objreader; <---- it does not like the datasourse...... any Ideas?
        DataGrid1.DataBind();
    }

Open in new window

0
 
LVL 30

Expert Comment

by:MlandaT
ID: 33559721
In a web based application, an objects lifetime is only during one execution. Once the page gets rendered and the user initiates another postback... object that were created on a prior call are no longer accessible. You need to recreate the reader object (or otherwise store it in a session variable - and should you decide to take this approach ... rather use a dataset as opposed to a datareader).

Try changing the GridPageIndexChanged method to this:

private void GridPageIndexChanged(object sender, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
     DataGrid1.CurrentPageIndex = e.NewPageIndex;
    work();
}

'Also make sure that you have changed the ASPX file accordingly (the part in BOLD in one of my previous posts here).
0
 

Author Comment

by:HawaiiDragon
ID: 33571855
This is the error I am getting now.....
Error      3      'System.EventArgs' does not contain a definition for 'NewPageIndex' and no extension method 'NewPageIndex' accepting a first argument of type 'System.EventArgs' could be found (are you missing a using directive or an assembly reference?)      C:\Documents and Settings\rshier\My Documents\Visual Studio 2008\ClassSchedules\RealDataGridDeleteMe3YouBetterFuckingWork.aspx.cs      43      40      C:\...\ClassSchedules\

ASPXCDE
_________________
<asp:DataGrid ID="DataGrid1" runat="server" AllowCustomPaging="True"
            AllowPaging="True" PageSize="10" AllowSorting="True"
            onselectedindexchanged="DataGrid1_SelectedIndexChanged" ShowFooter="True">
            <PagerStyle Mode="NumericPages" HorizontalAlign="Right" />
        </asp:DataGrid>

Codebhind (cs)
__________________________

using System;
using System.Configuration;
using System.Data.SqlClient;

public partial class RealDataGridDeleteMe3YouBetterFuckingWork : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {          }
    public void work()
    {
        System.Data.SqlClient.SqlConnection con4 = new System.Data.SqlClient.SqlConnection();
        con4.ConnectionString = ConfigurationManager.ConnectionStrings["strDBCourseStandard"].ConnectionString;
        con4.Open();
        string strSQL = "Select Distinct * from ClassScheduleView where SchoolYear = 2010 and Course_Abb = 'AR202' and Semester = 'F2Y2010' and Syllabus = 'Created - Approved' and Campus_Name = 'Fort Bliss'";
        SqlCommand objCommand = new SqlCommand(strSQL, con4);
        SqlDataReader objReader = objCommand.ExecuteReader();
        DataGrid1.VirtualItemCount = 10000;
        DataGrid1.DataSource = objReader;
        DataGrid1.DataBind();
        objReader.Close();
            }
    protected void Button1_Click(object sender, EventArgs e)
    {
        work();
    }
    protected void DataGrid1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataGrid1.CurrentPageIndex = e.NewPageIndex;
        work();
            }
}

please tell me why i am gettir errs for no extension methods
------- second error
Error      6      'ASP.realdatagriddeleteme3youbetterfuckingwork_aspx' does not contain a definition for 'GridPageIndexChanged' and no extension method 'GridPageIndexChanged' accepting a first argument of type 'ASP.realdatagriddeleteme3youbetterfuckingwork_aspx' could be found (are you missing a using directive or an assembly reference?)      C:\Documents and Settings\rshier\My Documents\Visual Studio 2008\ClassSchedules\RealDataGridDeleteMe3YouBetterFuckingWork.aspx      1      1      C:\...\ClassSchedules\

0
 
LVL 30

Expert Comment

by:MlandaT
ID: 33572402
protected void DataGrid1_SelectedIndexChanged(object sender, EventArgs e)


should be:

protected void DataGrid1_SelectedIndexChanged(object sender, DataGridPageChangedEventArgs e)


you are using the wrong event. See comment 33559721
0
 

Author Comment

by:HawaiiDragon
ID: 33576270
Now I am getting this error
---------------------------------------
Error      4      No overload for 'DataGrid1_SelectedIndexChanged' matches delegate 'System.EventHandler'      C:\Documents and Settings\rshier\My Documents\Visual Studio 2008\ClassSchedules\RealDataGridDeleteMe3YouBetterFuckingWork.aspx      1      1      C:\...\ClassSchedules\
--------------------------------------------------
aspx code
---------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RealDataGridDeleteMe3YouBetterFuckingWork.aspx.cs" Inherits="RealDataGridDeleteMe3YouBetterFuckingWork" %>

<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <br />
        <br />
        <asp:DataGrid ID="DataGrid1" runat="server" AllowCustomPaging="True"
            AllowPaging="True" PageSize="10" AllowSorting="True"
            OnPageIndexChanged="GridPageIndexChanged" onselectedindexchanged="DataGrid1_SelectedIndexChanged" ShowFooter="True">
            <PagerStyle Mode="NumericPages" HorizontalAlign="Right" />
        </asp:DataGrid>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
   
    </div>
    </form>
    <p>
        &nbsp;</p>
</body>
</html>
------------------------------------
cs code
---------------------------
using System;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.UI.WebControls.Adapters;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class RealDataGridDeleteMe3YouBetterFuckingWork : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {          }
    public void work()
    {
        System.Data.SqlClient.SqlConnection con4 = new System.Data.SqlClient.SqlConnection();
        con4.ConnectionString = ConfigurationManager.ConnectionStrings["strDBCourseStandard"].ConnectionString;
        con4.Open();
        string strSQL = "Select Distinct * from ClassScheduleView where SchoolYear = 2010 and Course_Abb = 'AR202' and Semester = 'F2Y2010' and Syllabus = 'Created - Approved' and Campus_Name = 'Fort Bliss'";
        SqlCommand objCommand = new SqlCommand(strSQL, con4);
        SqlDataReader objReader = objCommand.ExecuteReader();
        DataGrid1.VirtualItemCount = 10000;
        DataGrid1.DataSource = objReader;
        DataGrid1.DataBind();
        objReader.Close();
            }
    protected void Button1_Click(object sender, EventArgs e)
    {
        work();
    }
    protected void DataGrid1_SelectedIndexChanged(object sender, DataGridPageChangedEventArgs e)
    {
        DataGrid1.CurrentPageIndex = e.NewPageIndex;
        work();
            }
    protected void GridPageIndexChanged(object sender, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
    {
        DataGrid1.CurrentPageIndex = e.NewPageIndex;
        work();
    }
}
0
 
LVL 30

Expert Comment

by:MlandaT
ID: 33577668
sorry.. my bad...

you can now delete:
protected void DataGrid1_SelectedIndexChanged(object sender, DataGridPageChangedEventArgs e)
    {
        DataGrid1.CurrentPageIndex = e.NewPageIndex;
        work();
            }

and also remove this from the ASPX:
onselectedindexchanged="DataGrid1_SelectedIndexChanged"
0
 

Author Comment

by:HawaiiDragon
ID: 33578237
Still not paging through the records after the changes project builds but still not working? Did I forget anything?
0
 
LVL 30

Expert Comment

by:MlandaT
ID: 33578669
Does it stop in the GridPageIndexChanged method if you put a breakpoitn and try to page?
0
 

Author Comment

by:HawaiiDragon
ID: 33578789
No actually it will load watever page is selected from the bottom but will not load the information from that page. i.e. : on the list select page 6 and then it refreshes but the information that should be on page six is not displaying on the information on page one. I did add more code to see if that would help with the paging...

ASPX
____________

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RealDataGridDeleteMe3YouBetterFuckingWork.aspx.cs" Inherits="RealDataGridDeleteMe3YouBetterFuckingWork" %>

<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <br />
        <br />
        <asp:DataGrid ID="DataGrid1" runat="server" AllowCustomPaging="True"
            AllowPaging="True" PageSize="10" AllowSorting="True"
            OnPageIndexChanged="GridPageIndexChanged"  ShowFooter="True">
            <PagerStyle Mode="NumericPages" HorizontalAlign="Right" />
        </asp:DataGrid>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
        </div>
    </form>
    </body>
</html>

CS
_________________________________________
using System;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.UI.WebControls.Adapters;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class RealDataGridDeleteMe3YouBetterFuckingWork : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {          }
    public void work()
    {
        System.Data.SqlClient.SqlConnection con4 = new System.Data.SqlClient.SqlConnection();
        con4.ConnectionString = ConfigurationManager.ConnectionStrings["strDBCourseStandard"].ConnectionString;
        con4.Open();
        string strSQL = "Select Distinct * from ClassScheduleView where SchoolYear = 2010 and Course_Abb = 'AR202' and Semester = 'F2Y2010' and Syllabus = 'Created - Approved' and Campus_Name = 'Fort Bliss'";
        SqlCommand objCommand = new SqlCommand(strSQL, con4);
        SqlDataReader objReader = objCommand.ExecuteReader();
        DataGrid1.VirtualItemCount = 10000;
        DataGrid1.DataSource = objReader;
        Session["PageSize"] = 25;
        Session["PageStart"] = 0;
        DataGrid1.DataBind();
        objReader.Close();
            }
    protected void Button1_Click(object sender, EventArgs e)
    {
        work();
    }
   
    protected void GridPageIndexChanged(object sender, DataGridPageChangedEventArgs e)
    {
        DataGrid1.CurrentPageIndex = e.NewPageIndex;
        work();
    }
}
________________________________

No I was trying to use intCounter but it keptblowing up in my face. ....
0
 
LVL 30

Expert Comment

by:MlandaT
ID: 33578986
put breakpoint on this line and let's see if it works..         DataGrid1.CurrentPageIndex = e.NewPageIndex;
0
 

Author Comment

by:HawaiiDragon
ID: 33579038
It hits the breakpoint and states
DataGrid1.CurrentPageIndex {DataGrid1.CurrentPageIndex 0}
= e.NewPageIndex; {e.NewPageIndex 5}

I can screen share with you if you like.
0
 
LVL 30

Expert Comment

by:MlandaT
ID: 33579164
yes.. do that let's see...
0
 

Author Comment

by:HawaiiDragon
ID: 33579191
0
 

Author Comment

by:HawaiiDragon
ID: 33579253
hope you got that still awaiting a reply
0
 
LVL 30

Accepted Solution

by:
MlandaT earned 500 total points
ID: 33579666
take out AllowCustomPaging or set it to false :)


had not noticed that you had it set to true
0
 

Author Comment

by:HawaiiDragon
ID: 33580185
MlandaT IS A CODE GOD!!!
For those of you intrested
HERE IS THE FINAL CODE AND WALK THROUGH
ASPX
______________

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RealDataGridDeleteMe3YouBetterFuckingWork.aspx.cs" Inherits="RealDataGridDeleteMe3YouBetterFuckingWork" %>

<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <br />
        <br />
        <asp:DataGrid ID="DataGrid1" runat="server" AllowCustomPaging="False"
            AllowPaging="True" PageSize="10" AllowSorting="True"
            OnPageIndexChanged="GridPageIndexChanged"  ShowFooter="True">
            <PagerStyle Mode="NumericPages" HorizontalAlign="Right" />
        </asp:DataGrid>
        <br />
        <br />
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
   
    </div>
    </form>
    </body>
</html>
____________
aspx.cs
_______________
using System;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.UI.WebControls.Adapters;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class RealDataGridDeleteMe3YouBetterFuckingWork : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {          }
    public void work()
    {
        System.Data.SqlClient.SqlConnection con4 = new System.Data.SqlClient.SqlConnection();
        con4.ConnectionString = ConfigurationManager.ConnectionStrings["strDBCourseStandard"].ConnectionString;
        con4.Open();
        string strSQL = "Select Distinct * from ClassScheduleView where SchoolYear = 2010 and Course_Abb = 'AR202' and Semester = 'F2Y2010' and Syllabus = 'Created - Approved' and Campus_Name = 'Fort Bliss'";
        SqlCommand objCommand = new SqlCommand(strSQL, con4);
        SqlDataAdapter da = new SqlDataAdapter("Select Distinct * from ClassScheduleView where SchoolYear = 2010 and Course_Abb = 'AR202' and Semester = 'F2Y2010' and Syllabus = 'Created - Approved' and Campus_Name = 'Fort Bliss'", con4);
        System.Data.DataSet ds = new System.Data.DataSet();
        da.Fill(ds);
        DataGrid1.DataSource = ds.Tables[0];
        DataGrid1.DataBind();
            }
    protected void Button1_Click(object sender, EventArgs e)
    {
        work();
    }
   
    protected void GridPageIndexChanged(object sender, DataGridPageChangedEventArgs e)
    {
        DataGrid1.CurrentPageIndex = e.NewPageIndex;
        work();
    }
}

0
 

Author Closing Comment

by:HawaiiDragon
ID: 33580189
CODE GOD!!!!!! LISTEN TO HIM!!!!!
0
 
LVL 30

Expert Comment

by:MlandaT
ID: 33580317
HawaiiDragon! Looooooll..... thanks...... hahahaha.... best laugh I've had today!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
array not updating 8 41
PowerShell: Adding ToGB to a script 4 71
convert Absolute path to relative path of FTP location 7 49
Need Help With GDI+ in VB.Net 8 39
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…

737 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