Hello All,
I have a grid view that allows sorting, paging, editing, etc.
Under On Load event, if I check: if(!IsPostBack) { DisplayData(); }, the Edit portion works fine. However, the Sorting and paging portions won't. If I just call the DisplayData() method then the Edit portion doesn't work. I found out that when sorting the sorting value doesn't persist and I need to store it elsewhere when doing postbacks, but the code below is already doing it.
Any help is greatly appreciated.
//here is part of the code that I'm having problems with
private string GridViewSortDir ection
{
get { return ViewState["SortDirect ion"] as string ?? "ASC"; }
set { ViewState["SortDirect ion"] = value; }
}
private string GridViewSortExp ression
{
get { return ViewState["SortExpression "] as string ?? string.Empty; }
set { ViewState["SortExpression "] = value; }
}
private string GetSortDirectio n()
{
switch (GridViewSortDi rection)
{
case "ASC":
GridViewSortDir ection = "DESC";
break;
case "DESC":
GridViewSortDir ection = "ASC";
break;
}
return GridViewSortDir ection;
}
protected void GridView1_PageI ndexChanging(ob ject sender, GridViewPageEve ntArgs e)
{
GridView1.DataS ource = SortDataTable(G ridView1.DataSo urce as DataTable, true);
GridView1.PageI ndex = e.NewPageIndex;
GridView1.DataB ind();
}
protected DataView SortDataTable(D ataTable dataTable, bool isPageIndexChan ging)
{
if (dataTable != null)
{
DataView dataView = new DataView(dataTa ble);
if (GridViewSortEx pression != string.Empty)
{
if (isPageIndexCha nging)
{
dataView.Sort = string.Format(" {0} {1}", GridViewSortExp ression, GridViewSortDir ection);
}
else
{
dataView.Sort = string.Format(" {0} {1}", GridViewSortExp ression, GetSortDirectio n());
}
}
return dataView;
}
else
{
return new DataView();
}
}
protected void GridView1_Sorti ng(object sender, GridViewSortEve ntArgs e)
{
GridViewSortExp ression = e.SortExpressio n;
int pageIndex = GridView1.PageI ndex;
GridView1.DataS ource = SortDataTable(G ridView1.DataSo urce as DataTable, false);
GridView1.DataB ind();
GridView1.PageI ndex = pageIndex;
}
I have a grid view that allows sorting, paging, editing, etc.
Under On Load event, if I check: if(!IsPostBack) { DisplayData(); }, the Edit portion works fine. However, the Sorting and paging portions won't. If I just call the DisplayData() method then the Edit portion doesn't work. I found out that when sorting the sorting value doesn't persist and I need to store it elsewhere when doing postbacks, but the code below is already doing it.
Any help is greatly appreciated.
//here is part of the code that I'm having problems with
private string GridViewSortDir ection
{
get { return ViewState["SortDirect ion"] as string ?? "ASC"; }
set { ViewState["SortDirect ion"] = value; }
}
private string GridViewSortExp ression
{
get { return ViewState["SortExpression "] as string ?? string.Empty; }
set { ViewState["SortExpression "] = value; }
}
private string GetSortDirectio n()
{
switch (GridViewSortDi rection)
{
case "ASC":
GridViewSortDir ection = "DESC";
break;
case "DESC":
GridViewSortDir ection = "ASC";
break;
}
return GridViewSortDir ection;
}
protected void GridView1_PageI ndexChanging(ob ject sender, GridViewPageEve ntArgs e)
{
GridView1.DataS ource = SortDataTable(G ridView1.DataSo urce as DataTable, true);
GridView1.PageI ndex = e.NewPageIndex;
GridView1.DataB ind();
}
protected DataView SortDataTable(D ataTable dataTable, bool isPageIndexChan ging)
{
if (dataTable != null)
{
DataView dataView = new DataView(dataTa ble);
if (GridViewSortEx pression != string.Empty)
{
if (isPageIndexCha nging)
{
dataView.Sort = string.Format(" {0} {1}", GridViewSortExp ression, GridViewSortDir ection);
}
else
{
dataView.Sort = string.Format(" {0} {1}", GridViewSortExp ression, GetSortDirectio n());
}
}
return dataView;
}
else
{
return new DataView();
}
}
protected void GridView1_Sorti ng(object sender, GridViewSortEve ntArgs e)
{
GridViewSortExp ression = e.SortExpressio n;
int pageIndex = GridView1.PageI ndex;
GridView1.DataS ource = SortDataTable(G ridView1.DataSo urce as DataTable, false);
GridView1.DataB ind();
GridView1.PageI ndex = pageIndex;
}
Comment