Wrox Programmer Forums
|
VS.NET 2002/2003 Discussions about the Visual Studio.NET programming environment, the 2002 (1.0) and 2003 (1.1). ** Please don't post code questions here ** For issues specific to a particular language in .NET, please see the other forum categories.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VS.NET 2002/2003 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:40 AM
Registered User
 
Join Date: Oct 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default .NET 2 DataGrid w/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...
Any help or direction would be appreciated.

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
Duplicating datagrid rows using dataview batlou Visual Studio 2005 0 October 25th, 2006 11:41 AM
vb.net asp.net DataGrid krantips VS.NET 2002/2003 0 June 28th, 2006 01:22 AM
Converting a DataGrid.DataSource to a DataView demivolt ASP.NET 1.0 and 1.1 Professional 10 July 8th, 2005 12:00 PM
DataView vivi C# 4 November 10th, 2003 01:15 AM





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