?
Solved

How to set the current page index of a datagrid with delete functionality

Posted on 2005-03-24
20
Medium Priority
?
253 Views
Last Modified: 2010-04-07
Hi There,
I have a datagrid with paging feature. I set the page size to 3. So it loads 3 rows in a page. I also have the delete functionality for the datagrid. Can anybody suggest me how I can set the current page index properly so that when a row is deleted, the same page is loaded, except for the last row (loaded in a new page) in the datagrid which when deleted shud load the previous page?
example the datagrid has 7 rows and the rows are loaded into 3 pages, third page has only one row in it.
Thanks a lot.
Please let me know if the ques is not clear.
0
Comment
Question by:jackrock79
[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
  • 9
20 Comments
 
LVL 12

Expert Comment

by:laotzi2000
ID: 13622273
save the current page index in a session variable
after you delete a row, rebind the datagrid
if the page out of index?
  set the current page index to last page
else
  set the current page index to the page index you saved in session.
0
 
LVL 2

Author Comment

by:jackrock79
ID: 13622362
can u plz let me know how i can check if the page is out of index?
thanks.
0
 
LVL 12

Accepted Solution

by:
laotzi2000 earned 1000 total points
ID: 13622433
you can get the number of actual pages from
datagrid1.PageCount
so if index greater than or equal to dagagrid1.PageCount,
it's out of index
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:jackrock79
ID: 13622866
I have a ques for you regarding the same issue. After I delete a row, is it right that the pagecount doesn't change unless I bind the datagrid?
Thanks.
0
 
LVL 12

Expert Comment

by:laotzi2000
ID: 13622919
yeah, and the deleted row will not go if you dont rebind
0
 
LVL 2

Author Comment

by:jackrock79
ID: 13623050
Hi there,
I am getting an error "current page index shud be >=0 and <pagecount" if i delete the last row..Can you please let me know where is the proper place to check for that condition you mentioned...Thanks
This is how my code is...

Code in the delete handler....
{
.....I do the delete and then...

dgConstraints.EditItemIndex = -1;

this.ConstraintsDS = this.GetConstraintsDataSet(AccountId,strEngine);                                                                        
Constraints_Load();//This is the place where I bind the datagrid...
}


private void Constraints_Load()
            {
                  if(ConstraintsDS.Tables[0].Rows.Count > 0)
                  {
                        constraintId = Convert.ToInt32(ConstraintsDS.Tables[0].Rows[0].ItemArray[0].ToString());
                        this.RuleDR = this.RuleDS.Rule.FindByRuleId( constraintId );
                        this.ConstraintsDS.Merge(RuleDS);

                        this.ConstraintsDV = this.ConstraintsDT.DefaultView;
            
                        this.dgConstraints.Visible = true;
                        this.dgConstraints.DataSource = this.ConstraintsDV;
                        this.dgConstraints.DataBind();

                        this.constraintsDL_NavLeft.Visible = true;
                        this.constraintsDL_NavRight.Visible = true;
                        this.constraintsDL_NavTitle.Visible = true;

                        this.constraintsDL_NavLeft.Visible  = ( this.dgConstraints.CurrentPageIndex > 0 );
                        this.constraintsDL_NavRight.Visible = ( this.dgConstraints.CurrentPageIndex < ( this.dgConstraints.PageCount-1 ) );            

                        int startIndex = ( this.dgConstraints.CurrentPageIndex * this.dgConstraints.PageSize ) + 1;
                        int endIndex   = ( startIndex + this.dgConstraints.PageSize -1 );
                        if( endIndex > this.ConstraintsDV.Count )
                              endIndex = this.ConstraintsDV.Count;

                        this.constraintsDL_NavTitle.Text = String.Format( "{0}-{1} of {2}",
                              startIndex,
                              endIndex,
                              this.ConstraintsDV.Count );
                                                
                        this.lblNoConstraintsError.Visible = false;
                        
                        
                  }
}
0
 
LVL 12

Expert Comment

by:laotzi2000
ID: 13623165
You should put it right after databind
this.dgConstraints.DataBind();
0
 
LVL 2

Author Comment

by:jackrock79
ID: 13623206
That's what I did and it is giving me that error mentioned above(breaking at DataBind().) Any ideas as to why it is throwing that error.
Thanks.
0
 
LVL 12

Expert Comment

by:laotzi2000
ID: 13623242
what's your code?
I did not see it there.
0
 
LVL 2

Author Comment

by:jackrock79
ID: 13623268
if(this.dgConstraints.CurrentPageIndex == this.dgConstraints.PageCount -1)
                        {
                              dgConstraints.CurrentPageIndex--;

                        }
else
      this.dgConstraints.CurrentPageIndex = Convert.ToInt32(ViewState["CurrentPageIndex"]);
0
 
LVL 12

Expert Comment

by:laotzi2000
ID: 13623316
the first comparation is not correct.

should use the index in viewstate to compare
0
 
LVL 12

Expert Comment

by:laotzi2000
ID: 13623330
and should be
>= this.dgConstraints.PageCount
0
 
LVL 2

Author Comment

by:jackrock79
ID: 13623434
I changed this and it is breaking at DataBind() even before getting to this piece of code.

this.dgConstraints.DataSource = this.ConstraintsDV;
this.dgConstraints.DataBind();
if(Convert.ToInt32(ViewState["CurrentPageIndex"]) >= this.dgConstraints.PageCount)
                        {
                              dgConstraints.CurrentPageIndex--;

                        }
else
this.dgConstraints.CurrentPageIndex = Convert.ToInt32(ViewState["CurrentPageIndex"]);
0
 
LVL 12

Expert Comment

by:laotzi2000
ID: 13623551
instead of
dgConstraints.CurrentPageIndex--;
should be
this.dgConstraints.CurrentPageIndex = this.dgConstraints.PageCount - 1;

and for the delete, you should delete the row from the dataset instead of the datagrid
and then rebind the dataset to the datagrid
0
 
LVL 12

Expert Comment

by:laotzi2000
ID: 13623585
anyway, try set hte currentpageindex to 0 before the databind
and see what happens.
0
 
LVL 2

Author Comment

by:jackrock79
ID: 13623860
This is what I have...The error doesn't appear, but the problem is it loads the first page (after I delete the first row in the last page), even though the current page index changes after the control is bound. I am unable to figure out why? Probably becoz the currentpageindex is set to 0 before the control is bound..Any suggestions plz...


this.dgConstraints.CurrentPageIndex = 0;
                        this.dgConstraints.Visible = true;
                        this.dgConstraints.DataSource = this.ConstraintsDV;
                        this.dgConstraints.DataBind();

                        if(Convert.ToInt32(ViewState["CurrentPageIndex"]) >= this.dgConstraints.PageCount)
                        {
                              dgConstraints.CurrentPageIndex=this.dgConstraints.PageCount - 1;

                        }
                        else
                              this.dgConstraints.CurrentPageIndex = Convert.ToInt32(ViewState["CurrentPageIndex"]);
0
 
LVL 12

Expert Comment

by:laotzi2000
ID: 13623910
yeah, you should store the currentpageindex to viewstate in your page index change event handler.
0
 
LVL 2

Author Comment

by:jackrock79
ID: 13623943
I am doing that already...but becoz i am setting the current page index to 0 before binding the datagrid, it is loading the first page which is not supposed to be happening. So what can be done to overcome this?
Thanks.
0
 
LVL 12

Expert Comment

by:laotzi2000
ID: 13623976
you can change the current index and rebind it
0
 
LVL 2

Author Comment

by:jackrock79
ID: 13623994
yeah
That's what I did and it is working fine.
Thanks for all your help.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
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