Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2005-03-24
20
Medium Priority
?
264 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
  • 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
Industry Leaders: 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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 this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month12 days, 3 hours left to enroll

564 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