Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > .NET 2.0 and Visual Studio. 2005 > Visual Studio 2005
| Search | Today's Posts | Mark Forums Read
Visual Studio 2005 For discussing Visual Studio 2005. Please post code questions about a specific language (C#, VB, ASP.NET, etc) in the correct language forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Studio 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 25th, 2006, 11:41 AM
Registered User
 
Join Date: Oct 2006
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Duplicating datagrid rows using dataview

I am having a problem with the datagrid. I am using .NET 2.0 Framwork with VS 2005 ASP/C#. I have a dataview that I am binding to the datagrid. It also has some column sorting options and is using paging sized at 50 per page.
When you load the dataview and then bind it to the grid it works fine and you can page through the dataset. So lets say that I am on page 3 of 10 and I decide to change the sorting to the date rather than description field. At first it is fine but then if you go to the next page and back it will duplicate some rows. I can do the same thing again and the one that was duplicated is now singular and a different row might be duplicated or none at all.
Here are the two functions that handle both paging and sorting...

public void dgCItems_SortCommand(object source, DataGridSortCommandEventArgs e)
        {
            //Reset the current Page to 1
            dgCItems.CurrentPageIndex = 0;
            ViewState["currentpage"] = 0;

            // We persist the sort expression to the ViewState object
            // so that we keep the sort field and order during paging.

            // Reset the sort direction if this is a request to sort
            // on a different field than the last request.
            if (ViewState["sortfield"] != null &&
                e.SortExpression != ViewState["sortfield"].ToString())
            {
                ViewState["sortdirection"] = null;
            }


            // Setup the sortfield object
            ViewState.Add("sortfield", e.SortExpression);


            // Determine the correct sort direction
            if (ViewState["sortdirection"] == null)
                ViewState.Add("sortdirection", "ASC");
            else
            {
                if (ViewState["sortdirection"].ToString() == "ASC")
                    ViewState["sortdirection"] = "DESC";
                else
                    ViewState["sortdirection"] = "ASC";
            }

            DataView dv = (DataView)Session["CItemsDV"];

            if (e.SortExpression.Length > 0)
            {
                dv.Sort = e.SortExpression + " " +
                    ViewState["sortdirection"].ToString();
            }

            this.dgCItems.DataSource = dv;
            this.dgCItems.DataBind();


            SetGridPaging(false);

        }

        public void SetGridPaging(bool reset)
        {
            if (reset == true)
            {
                lblCurrentPage.Text = "Page 1 of " + dgCItems.PageCount;
            }
            else
            {
                try
                {
                    lblCurrentPage.Text = "Page " + (Convert.ToInt32(ViewState["currentpage"].ToString()) + 1) +
                        " of " + dgCItems.PageCount;
                }
                catch
                {
                    lblCurrentPage.Text = "Page 1 of " + dgCItems.PageCount;
                }
            }

            //Set all buttons to visible then turn off based on selection and page
            btnFirst.Visible = true;
            btnNext.Visible = true;
            btnPrevious.Visible = true;
            btnLast.Visible = true;

            if (dgCItems.CurrentPageIndex == 0)
            {
                if (dgCItems.PageCount > 1)
                {
                    btnFirst.Visible = false;
                    btnPrevious.Visible = false;
                    btnNext.Visible = true;
                    btnLast.Visible = true;
                }
                else
                {
                    btnFirst.Visible = false;
                    btnPrevious.Visible = false;
                    btnNext.Visible = false;
                    btnLast.Visible = false;
                }
            }
            if (dgCItems.CurrentPageIndex == dgCItems.PageCount - 1)
            {
                if (dgCItems.PageCount > 1)
                {
                    btnFirst.Visible = true;
                    btnPrevious.Visible = true;
                    btnNext.Visible = false;
                    btnLast.Visible = false;
                }
                else
                {
                    btnFirst.Visible = false;
                    btnPrevious.Visible = false;
                    btnNext.Visible = false;
                    btnLast.Visible = false;
                }
            }
        }






Similar Threads
Thread Thread Starter Forum Replies Last Post
Datagrid or Dataview cp75 Beginning PHP 2 September 26th, 2008 10:56 AM
.NET 2 DataGrid w/DataView batlou VS.NET 2002/2003 0 October 25th, 2006 11:40 AM
Converting a DataGrid.DataSource to a DataView demivolt ASP.NET 1.0 and 1.1 Professional 10 July 8th, 2005 12:00 PM
rows in Datagrid drachx General .NET 0 October 20th, 2004 08:02 PM
duplicating rows msrnivas SQL Server 2000 3 September 30th, 2004 07:41 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.