Can't get DataGrid to page. in C#

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

HawaiiDragonAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MlandaTCommented:
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
HawaiiDragonAuthor Commented:
how would I do this in C# I dont know VB
0
MlandaTCommented:
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
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Akin DeluI’m a software engineer, tech enthusiast and UI/UX lover. Commented:
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
HawaiiDragonAuthor Commented:
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
MlandaTCommented:
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
HawaiiDragonAuthor Commented:
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
MlandaTCommented:
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
HawaiiDragonAuthor Commented:
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
MlandaTCommented:
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
HawaiiDragonAuthor Commented:
Still not paging through the records after the changes project builds but still not working? Did I forget anything?
0
MlandaTCommented:
Does it stop in the GridPageIndexChanged method if you put a breakpoitn and try to page?
0
HawaiiDragonAuthor Commented:
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
MlandaTCommented:
put breakpoint on this line and let's see if it works..         DataGrid1.CurrentPageIndex = e.NewPageIndex;
0
HawaiiDragonAuthor Commented:
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
MlandaTCommented:
yes.. do that let's see...
0
HawaiiDragonAuthor Commented:
0
HawaiiDragonAuthor Commented:
hope you got that still awaiting a reply
0
MlandaTCommented:
take out AllowCustomPaging or set it to false :)


had not noticed that you had it set to true
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HawaiiDragonAuthor Commented:
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
HawaiiDragonAuthor Commented:
CODE GOD!!!!!! LISTEN TO HIM!!!!!
0
MlandaTCommented:
HawaiiDragon! Looooooll..... thanks...... hahahaha.... best laugh I've had today!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.