Click here to Skip to main content
15,909,591 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Friends,

I enable paging and use following event
C#
protected void gvReprintEmail_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvReprintEmail.PageIndex = e.NewPageIndex;
        DataTable dt1 = (DataTable)Session["dt"];
        gvReprintEmail.DataSource = dt1;
        gvReprintEmail.DataBind();
    }

But when i want to get any value from gridview then i got exception because in rowcommand event i get value of row from current event. So there is exception like "Index was out of range. Must be non-negative and less than the size of the collection."

I use page size 3 and in 2nd and afterward pages i got exception like this.

here is one of column of gridview

<itemtemplate>
<asp:Label runat="server" ID="lblMessage" Text='<%# Server.HtmlDecode(Eval("Body").ToString()) %>' >




following is rowcommand event where i got exception
in first page there is no problem in this, but afterwards page when i click link given in gridview then there will exception

protected void gvReprintEmail_RowCommand(object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument); <--on 2nd page first row i click so index will be 3 because page size 3

Label lbl = (Label)gvReprintEmail.Rows[index].FindControl("lblMessage"); <--exception
Session["SentDate"] = gvReprintEmail.Rows[index].Cells[0].Text;
Session["Subject"] = gvReprintEmail.Rows[index].Cells[1].Text;
Session["Message"] = lbl.Text;
OpenNewWindow("PrintEmail.aspx");
}


Kindly help me. Thanks in advance.
Posted
Updated 7-Aug-11 19:42pm
v5

 
Share this answer
 
C#
private void BindData()
{
    string connectionString = "Server=localhost;" + 
           "Database=Northwind;Trusted_Connection=true";
    SqlConnection myConnection = new SqlConnection(connectionString);
    SqlCommand myCommand = new SqlCommand("usp_GetProducts", 
                                           myConnection);
    myCommand.CommandType = CommandType.StoredProcedure;
    myCommand.Parameters.AddWithValue("@startRowIndex", 
                                      currentPageNumber);
    myCommand.Parameters.AddWithValue("@maximumRows", PAGE_SIZE);
    myCommand.Parameters.Add("@totalRows", SqlDbType.Int, 4);
    myCommand.Parameters["@totalRows"].Direction = 
                       ParameterDirection.Output;
    SqlDataAdapter ad = new SqlDataAdapter(myCommand);
    DataSet ds = new DataSet();
    ad.Fill(ds);
    gvProducts.DataSource = ds;
    gvProducts.DataBind();
    // get the total rows 
    double totalRows = (int)myCommand.Parameters["@totalRows"].Value;
    lblTotalPages.Text = CalculateTotalPages(totalRows).ToString();
    lblCurrentPage.Text = currentPageNumber.ToString(); 
    if (currentPageNumber == 1)
    {
        Btn_Previous.Enabled = false;
        if (Int32.Parse(lblTotalPages.Text) > 0)
        {
            Btn_Next.Enabled = true;
        }
        else
            Btn_Next.Enabled = false;
    }
    else
    {
        Btn_Previous.Enabled = true;
        if (currentPageNumber == Int32.Parse(lblTotalPages.Text))
            Btn_Next.Enabled = false;
        else Btn_Next.Enabled = true; 
    }
}



Calculate total rows in table

C#
private int CalculateTotalPages(double totalRows)
{
    int totalPages = (int)  Math.Ceiling(totalRows / PAGE_SIZE);
    return totalPages; 
}



Make two buttons(next/Previous) and call this method on their click
C#
protected void ChangePage(object sender, CommandEventArgs e)
{
    
    switch (e.CommandName)
    {
        case "Previous":
            currentPageNumber = Int32.Parse(lblCurrentPage.Text) - 1;
            break; 
        case "Next":
            currentPageNumber = Int32.Parse(lblCurrentPage.Text) + 1; 
            break; 
    }
    BindData();
}
 
Share this answer
 
Thanks to all for giving your valuable suggestion.

But I enable paging then also I cant access another pages so following code is useful in pageindex changing event.

protected void gvReprintEmail_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvReprintEmail.PageIndex = e.NewPageIndex;
DataTable dt1 = (DataTable)Session["dt"];
gvReprintEmail.DataSource = dt1;
gvReprintEmail.DataBind();
}
 
Share this answer
 
Comments
Christian Graus 8-Aug-11 1:25am    
Don't push 'answer' to comment. See how I pushed the 'comment' button ? You can't access the data in the other pages if you do paging properly. If you do it the way you show, you'll probably find that the entire data source is being written to view state. Real programmers don't do that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900