.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;
}
}
}
|