Page 1 of 1

Sorting by related entity's property in DataGridView

Posted: Thu Jun 04, 2015 11:15 am
by jguengerich
If I have the following cb:AbleGridView (or asp:DataGridView) and asp:ObjectDataSource, is there a way to sort the grid by the Product.Sku property?

Code: Select all

        <cb:AbleGridView ID="SpecialGrid" runat="server" AutoGenerateColumns="False" DataKeyNames="SpecialId" DataSourceID="SpecialDs" 
            AllowSorting="True" SkinID="PagedList" AllowPaging="True" PageSize="20" PagerSettings-Position="TopAndBottom" EnableViewState="false" Width="100%">
            <Columns>
                <asp:TemplateField HeaderText="Part #">
                    <ItemTemplate>
                        <asp:Label ID="Sku" runat="server" Text='<%# Eval("Product.Sku") %>'></asp:Label>
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="Center" />
                </asp:TemplateField>
                <%-- other fields --%>
            </Columns>
        </cb:AbleGridView>
    <asp:ObjectDataSource ID="SpecialDs" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="LoadAll" TypeName="CommerceBuilder.Products.SpecialDataSource"
        DataObjectTypeName="CommerceBuilder.Products.Special" DeleteMethod="Delete" SortParameterName="sortExpression" EnablePaging="true">
    </asp:ObjectDataSource>
Note that setting the Text property of the ItemTemplate to '<%# Eval("Product.Sku") %>' shows the Sku correctly.
However, setting the SortExpression property of the TemplateField to "Product.Sku" like this:

Code: Select all

                <asp:TemplateField HeaderText="Part #" SortExpression="Product.Sku">
results in an error:
[QueryException: could not resolve property: Product.Sku of: CommerceBuilder.Products.Special]
NHibernate.Persister.Entity.AbstractPropertyMapping.ToColumns(String alias, String propertyName) +372
NHibernate.Persister.Entity.BasicEntityPropertyMapping.ToColumns(String alias, String propertyName) +266
NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetColumnAliasesUsingProjection(ICriteria subcriteria, String propertyName) +374
NHibernate.Criterion.Order.ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery) +380
NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetOrderBy() +235
NHibernate.Loader.Criteria.CriteriaJoinWalker..ctor(IOuterJoinLoadable persister, CriteriaQueryTranslator translator, ISessionFactoryImplementor factory, ICriteria criteria, String rootEntityName, IDictionary`2 enabledFilters) +495
NHibernate.Loader.Criteria.CriteriaLoader..ctor(IOuterJoinLoadable persister, ISessionFactoryImplementor factory, CriteriaImpl rootCriteria, String rootEntityName, IDictionary`2 enabledFilters) +258
NHibernate.Impl.SessionImpl.List(CriteriaImpl criteria, IList results) +672
NHibernate.Impl.CriteriaImpl.List(IList results) +63
NHibernate.Impl.CriteriaImpl.List() +79
CommerceBuilder.DomainModel.RepositoryWithTypedId`2.LoadAll(String sortExpression, Int32 maximumRows, Int32 startRowIndex) +188
CommerceBuilder.DomainModel.DataSourceWithTypedId`3.LoadAll(Int32 maximumRows, Int32 startRowIndex, String sortExpression) +93

Re: Sorting by related entity's property in DataGridView

Posted: Thu Jun 04, 2015 9:01 pm
by Naveed
If you have source code of CommerceBuilder API, then you can define a new property to CommerceBuilder.Products.Special class as under:

Code: Select all

/// <summary>
        /// Gets the associated product sku
        /// </summary>
        public virtual string ProductSku
        {
            get { return this.Product.Sku; }
        }
After this compile the CommerceBuilder API and update the cb:AbleGridView to use the newly defined property "ProductSku":

Code: Select all

<asp:TemplateField HeaderText="Part #" SortExpression="ProductSku">
                    <ItemTemplate>
                        <asp:Label ID="Sku" runat="server" Text='<%# Eval("ProductSku") %>'></asp:Label>
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="Center" />
                </asp:TemplateField>

Re: Sorting by related entity's property in DataGridView

Posted: Fri Jun 05, 2015 5:43 am
by jguengerich
I still get the same error (only difference is "ProductSku" instead of "Product.Sku"):
[QueryException: could not resolve property: ProductSku of: CommerceBuilder.Products.Special]
NHibernate.Persister.Entity.AbstractEntityPersister.GetSubclassPropertyTableNumber(String propertyPath) +389
NHibernate.Persister.Entity.BasicEntityPropertyMapping.ToColumns(String alias, String propertyName) +46
NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetColumnAliasesUsingProjection(ICriteria subcriteria, String propertyName) +374
NHibernate.Criterion.Order.ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery) +380
NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetOrderBy() +235
NHibernate.Loader.Criteria.CriteriaJoinWalker..ctor(IOuterJoinLoadable persister, CriteriaQueryTranslator translator, ISessionFactoryImplementor factory, ICriteria criteria, String rootEntityName, IDictionary`2 enabledFilters) +495
NHibernate.Loader.Criteria.CriteriaLoader..ctor(IOuterJoinLoadable persister, ISessionFactoryImplementor factory, CriteriaImpl rootCriteria, String rootEntityName, IDictionary`2 enabledFilters) +258
NHibernate.Impl.SessionImpl.List(CriteriaImpl criteria, IList results) +672
NHibernate.Impl.CriteriaImpl.List(IList results) +63
NHibernate.Impl.CriteriaImpl.List() +79
CommerceBuilder.DomainModel.RepositoryWithTypedId`2.LoadAll(String sortExpression, Int32 maximumRows, Int32 startRowIndex) +188
CommerceBuilder.DomainModel.DataSourceWithTypedId`3.LoadAll(Int32 maximumRows, Int32 startRowIndex, String sortExpression) +93
I think it is because it is trying to put "ProductSku" (or "Product.Sku") in the query, and the table doesn't have a column with that name.

Will I just have to create a custom Nhibernate query in a new method to use as the data source's "SelectMethod"?

EDIT: The correct value is shown in the ItemTemplate using "ProductSku", but the error still occurs when trying to sort.

Re: Sorting by related entity's property in DataGridView

Posted: Fri Jun 05, 2015 5:53 am
by Naveed
I am sorry that it worked partially. You are right that it is trying to use the "ProductSku" in SQL and respective column do not exists.
Will I just have to create a custom Nhibernate query in a new method to use as the data source's "SelectMethod"?
If sorting by product sku is important for you then you should go for the custom nhibernate query, and use it as data source's "SelectMethod".