Hi
I have a page set up as below. All methods are correctly wired to the grid in the .aspx page. as shown here
<telerik:RadGrid ID="FilesGrid" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" GridLines="None"
onpageindexchanged="FilesGrid_PageIndexChanged"
onsortcommand="FilesGrid_SortCommand" Skin="Outlook"
style="font-style: italic"
onitemdatabound="FilesGrid_ItemDataBound">
<MasterTableView AllowNaturalSort="false">
</MasterTableView>
</telerik:RadGrid>
However I am getting very inconsistent behaviour. I am not sure if it is something silly I am doing or is a result of using a nhibernate dataset.
For most fields field that map directly to a read/write nhibernate property it all works ok. However some columns need to click twice to sort them. Then I have what is a calculated nhinernate property of type sting - this sorts but will only sort one way??
Then when I try to use the standard navigator to page, it will go to page 2 but gets stuck there.
Any ideas? Also how - if possible - would I use a linked nhibernate property to sort.ie Status.StatusDescription? If not possible could I implement custom sorting? But how to keep the sorted columns highlighted even when underlying field being sorted is different?
Any pointers as always gratefully appreciated. Really at end of my tether and this is slowing the project right down, if can't fix it will have to find another solution
Thanks in advance
Kieran
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NHibernate.Cfg;
using Telerik.Web.UI;
using NHibernate;
public partial class Files : BasePageMinor
{
private string Sorting = string.Empty;
private string Sortdir = string.Empty;
protected void Page_Init(object sender, EventArgs e)
{
State.UseState = true;
Master.SetPage();
}
protected void Page_Load(object sender, EventArgs e)
{
SetUpGrid();
}
private void SetUpGrid()
{
FilesGrid.MasterTableView.Columns.Clear();
FilesGrid.MasterTableView.DataKeyNames = new string[] { "FileId", "FileRef" };
GridBoundColumn _col;
string sortField = State.FileOrderField??string.Empty;
string sortDir = State.FileOrderDir ?? "asc";
GridButtonColumn _imagecol = new GridButtonColumn();
FilesGrid.MasterTableView.Columns.Add(_imagecol);
_imagecol.UniqueName = "fileimagestatus";
_imagecol.ButtonType = GridButtonColumnType.ImageButton;
_imagecol.Resizable = false;
_imagecol.ItemStyle.Width = 10;
_col = new GridBoundColumn();
FilesGrid.MasterTableView.Columns.Add(_col);
_col.DataField = "BorrowStatusString";
_col.UniqueName = "BorrowStatusString";
_col.DataType = Type.GetType("System.String");
_col.Display = false;
foreach (FieldDisplay fd in new FileDisplay(s.GetParams()))
{
if (!(fd.Display == FieldShow.Show))
continue;
if (sortField == string.Empty)
sortField = fd.nHibernateMapping;
_col = new GridBoundColumn();
FilesGrid.MasterTableView.Columns.Add(_col);
_col.HeaderText = fd.HeaderDisplay;
_col.DataField = fd.nHibernateMapping;
_col.UniqueName = fd.nHibernateMapping;
_col.DataType = Type.GetType(fd.DataType);
_col.AllowSorting = true;
if (fd.DataType.Equals("System.DateTime"))
_col.DataFormatString = "{0:dd/MM/yyyy}";
}
if (!IsPostBack)
SortGird(sortField, sortDir);
}
private void SortGird(string expression, string direction)
{
GridSortExpression sort = new GridSortExpression();
sort.FieldName = expression;
sort.SortOrder = (direction == string.Empty || direction == "asc") ? GridSortOrder.Ascending
: GridSortOrder.Descending;
FilesGrid.MasterTableView.SortExpressions.Add(sort);
State.FileOrderDir = direction == string.Empty ? "asc" : direction;
State.FileOrderField = expression;
}
protected void FilesGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
{
if (State.SearchGroup == QuickSearchGroup.loan)
FilesGrid.DataSource = s.GetFilesLoans(State);
else
FilesGrid.DataSource = s.GetFiles(State);
}
protected void FilesGrid_PageIndexChanged(object source, GridPageChangedEventArgs e)
{
FilesGrid.CurrentPageIndex = e.NewPageIndex;
}
protected void FilesGrid_SortCommand(object source, GridSortCommandEventArgs e)
{
State.FileOrderDir = e.NewSortOrder == GridSortOrder.Descending ? "desc" : "asc";
State.FileOrderField = e.SortExpression;
if (!e.Item.OwnerTableView.SortExpressions.ContainsExpression(e.SortExpression))
{
GridSortExpression sortExpr = new GridSortExpression();
sortExpr.FieldName = e.SortExpression;// StandardFields.GetnHibernatePropertyName(e.SortExpression);
sortExpr.SortOrder = e.NewSortOrder;
e.Item.OwnerTableView.SortExpressions.AddSortExpression(sortExpr);
}
}
protected void FilesGrid_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem dataItem = e.Item as GridDataItem;
object status = dataItem["BorrowStatusString"].Text;
(dataItem["fileimagestatus"].Controls[0] as ImageButton).ImageUrl = string.Format("~/images/Borrow{0}.bmp", status);
}
}
}
I have a page set up as below. All methods are correctly wired to the grid in the .aspx page. as shown here
<telerik:RadGrid ID="FilesGrid" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" GridLines="None"
onpageindexchanged="FilesGrid_PageIndexChanged"
onsortcommand="FilesGrid_SortCommand" Skin="Outlook"
style="font-style: italic"
onitemdatabound="FilesGrid_ItemDataBound">
<MasterTableView AllowNaturalSort="false">
</MasterTableView>
</telerik:RadGrid>
However I am getting very inconsistent behaviour. I am not sure if it is something silly I am doing or is a result of using a nhibernate dataset.
For most fields field that map directly to a read/write nhibernate property it all works ok. However some columns need to click twice to sort them. Then I have what is a calculated nhinernate property of type sting - this sorts but will only sort one way??
Then when I try to use the standard navigator to page, it will go to page 2 but gets stuck there.
Any ideas? Also how - if possible - would I use a linked nhibernate property to sort.ie Status.StatusDescription? If not possible could I implement custom sorting? But how to keep the sorted columns highlighted even when underlying field being sorted is different?
Any pointers as always gratefully appreciated. Really at end of my tether and this is slowing the project right down, if can't fix it will have to find another solution
Thanks in advance
Kieran
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NHibernate.Cfg;
using Telerik.Web.UI;
using NHibernate;
public partial class Files : BasePageMinor
{
private string Sorting = string.Empty;
private string Sortdir = string.Empty;
protected void Page_Init(object sender, EventArgs e)
{
State.UseState = true;
Master.SetPage();
}
protected void Page_Load(object sender, EventArgs e)
{
SetUpGrid();
}
private void SetUpGrid()
{
FilesGrid.MasterTableView.Columns.Clear();
FilesGrid.MasterTableView.DataKeyNames = new string[] { "FileId", "FileRef" };
GridBoundColumn _col;
string sortField = State.FileOrderField??string.Empty;
string sortDir = State.FileOrderDir ?? "asc";
GridButtonColumn _imagecol = new GridButtonColumn();
FilesGrid.MasterTableView.Columns.Add(_imagecol);
_imagecol.UniqueName = "fileimagestatus";
_imagecol.ButtonType = GridButtonColumnType.ImageButton;
_imagecol.Resizable = false;
_imagecol.ItemStyle.Width = 10;
_col = new GridBoundColumn();
FilesGrid.MasterTableView.Columns.Add(_col);
_col.DataField = "BorrowStatusString";
_col.UniqueName = "BorrowStatusString";
_col.DataType = Type.GetType("System.String");
_col.Display = false;
foreach (FieldDisplay fd in new FileDisplay(s.GetParams()))
{
if (!(fd.Display == FieldShow.Show))
continue;
if (sortField == string.Empty)
sortField = fd.nHibernateMapping;
_col = new GridBoundColumn();
FilesGrid.MasterTableView.Columns.Add(_col);
_col.HeaderText = fd.HeaderDisplay;
_col.DataField = fd.nHibernateMapping;
_col.UniqueName = fd.nHibernateMapping;
_col.DataType = Type.GetType(fd.DataType);
_col.AllowSorting = true;
if (fd.DataType.Equals("System.DateTime"))
_col.DataFormatString = "{0:dd/MM/yyyy}";
}
if (!IsPostBack)
SortGird(sortField, sortDir);
}
private void SortGird(string expression, string direction)
{
GridSortExpression sort = new GridSortExpression();
sort.FieldName = expression;
sort.SortOrder = (direction == string.Empty || direction == "asc") ? GridSortOrder.Ascending
: GridSortOrder.Descending;
FilesGrid.MasterTableView.SortExpressions.Add(sort);
State.FileOrderDir = direction == string.Empty ? "asc" : direction;
State.FileOrderField = expression;
}
protected void FilesGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
{
if (State.SearchGroup == QuickSearchGroup.loan)
FilesGrid.DataSource = s.GetFilesLoans(State);
else
FilesGrid.DataSource = s.GetFiles(State);
}
protected void FilesGrid_PageIndexChanged(object source, GridPageChangedEventArgs e)
{
FilesGrid.CurrentPageIndex = e.NewPageIndex;
}
protected void FilesGrid_SortCommand(object source, GridSortCommandEventArgs e)
{
State.FileOrderDir = e.NewSortOrder == GridSortOrder.Descending ? "desc" : "asc";
State.FileOrderField = e.SortExpression;
if (!e.Item.OwnerTableView.SortExpressions.ContainsExpression(e.SortExpression))
{
GridSortExpression sortExpr = new GridSortExpression();
sortExpr.FieldName = e.SortExpression;// StandardFields.GetnHibernatePropertyName(e.SortExpression);
sortExpr.SortOrder = e.NewSortOrder;
e.Item.OwnerTableView.SortExpressions.AddSortExpression(sortExpr);
}
}
protected void FilesGrid_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem dataItem = e.Item as GridDataItem;
object status = dataItem["BorrowStatusString"].Text;
(dataItem["fileimagestatus"].Controls[0] as ImageButton).ImageUrl = string.Format("~/images/Borrow{0}.bmp", status);
}
}
}