This is a migrated thread and some comments may be shown as answers.

Error when sorting columns with spaces in SortExpression

5 Answers 355 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Billy
Top achievements
Rank 2
Billy asked on 05 May 2014, 03:39 PM
Hello,

I have a grid that is already defined, with several columns, in the markup. Based on the data returned I may have to dynamically add additional columns to this grid. The additional columns can potentially have spaces in them. When setting the SortExpression property on the dynamically created GridBoundColumn I am wrapping the string value in brackets, for example GridBoundColumn.SortExpression = "[Dynamic Column Name]".

When I sort by this column I am getting the following error: [Dynamic Column Name] is neither a DataColumn nor a DataRelation for table Table.

I am creating the columns on page load when IsPostBack = false.

Any idea on how to get these things to sort?

Thanks!

5 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 06 May 2014, 07:25 AM
Hi Billy Hasan,

Make sure that the SortExpression that you set should match with the DataField name of your column. If your column DataField is "ColumnName" then SortExpression should be "ColumnName" and not "Column Name".
Please make changes and see if it helps. Provide your code snippet for further help.

Thanks,
Princy
0
Billy
Top achievements
Rank 2
answered on 07 May 2014, 04:39 PM
Both SortExpression and DataField match. There is indeed a space in the name, so your example of "Column Name" would be correct. In the example below sorting on the "Dynamic Column" column throws an error.

Here is the mark up:

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" AllowSorting="true">
    <MasterTableView>
        <Columns>
            <telerik:GridBoundColumn SortExpression="Column1" HeaderText="Column 1" DataField="Column1" />
            <telerik:GridBoundColumn SortExpression="Column2" HeaderText="Column 2" DataField="Column2" />
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

Here is the .NET code:

Private _GridData As DataSet
    Private ReadOnly Property GridData As DataSet
        Get
            If _GridData Is Nothing Then _GridData = GetData()
            Return _GridData
        End Get
    End Property

    Protected Overrides Sub OnLoadData(e As EventArgs)
        MyBase.OnLoadData(e)
        '
        Dim bc As Telerik.Web.UI.GridBoundColumn
        Dim ds As DataSet = Me.GridData
        Dim colname As String
        '
        For colCount = Me.RadGrid1.Columns.Count To ds.Tables(0).Columns.Count - 1
            bc = New Telerik.Web.UI.GridBoundColumn
            colname = ds.Tables(0).Columns(colCount).ColumnName
            '
            Me.RadGrid1.MasterTableView.Columns.Add(bc)
            '
            bc.HeaderText = colname
            bc.DataField = String.Format("[{0}]", colname)
            bc.SortExpression = String.Format("[{0}]", colname)
        Next
    End Sub

    Private Sub RadGrid1_NeedDataSource(sender As Object, e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource
        Me.RadGrid1.DataSource = Me.GridData
    End Sub

    Private Function GetData() As DataSet
        Dim ds As New DataSet
        '
        SqlHelper.ExecuteDataset(ds, MyBase.Location.ConnectionString, CommandType.StoredProcedure, "LOGGER_Test",
                                 New SqlParameter("@SortExpression", Me.RadGrid1.MasterTableView.SortExpressions.GetSortString))
        '
        Return ds
    End Function

And here is a simplified version of the SQL within the SProc the data is coming from:

CREATE TABLE #tData(Column1 INT, Column2 INT, [Dynamic Column] INT)
INSERT INTO #tData(Column1, Column2, [Dynamic Column])
SELECT 1, 2, 3
UNION
SELECT 3, 4, 5

SELECT @SortExpression = ISNULL(@SortExpression, 'Column1')

DECLARE @sql VARCHAR(MAX)

SET @sql = '
WITH tRawData AS(
SELECT *, RowNumber = ROW_NUMBER() OVER (ORDER BY '+@SortExpression+')
FROM #tData)

SELECT * FROM tRawData'

EXECUTE(@sql)







0
Kostadin
Telerik team
answered on 08 May 2014, 07:50 AM
Hello Billy,

Note that the SortExpression property representing a filed-name from the DataSource that should be used when grid sorts by this column. Basically this property is used when sorting a TemplateColumn. When you have a bound column it is not mandatory to specify the sort expression as it is automatically applied. On the other hand if you want to sort some of the column programmatically then you have to create instances of the GridSortExpression class and adding them to the SortExpressions collection. More information could be found at the following help article.

Regards,
Kostadin
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Billy
Top achievements
Rank 2
answered on 09 May 2014, 03:18 PM
That is good to know that I do not have to set the sort expression however not setting the sort expression does not fix the error I am getting.

I think it has something to do w/ the way the grid is internaly sorting the data? The RadGrid1.MasterTableView.SortExpressions.GetSortString value seems to be correct.

Here is the error I get with EnableLinqExpressions = True
[ArgumentException: [Dynamic Column] is neither a DataColumn nor a DataRelation for table Table.]
   System.Data.DataRowView.get_Item(String property) +2125376
   lambda_method(Closure , DataRowView ) +32
   System.Linq.EnumerableSorter`2.ComputeKeys(TElement[] elements, Int32 count) +148
   System.Linq.EnumerableSorter`1.Sort(TElement[] elements, Int32 count) +38
   System.Linq.<GetEnumerator>d__0.MoveNext() +236
   Telerik.Web.UI.GridDataTableFromEnumerable.FillDataTableFromEnumerable(IQueryable enumerable) +1082
   Telerik.Web.UI.GridDataTableFromEnumerable.FillData35() +3732
   Telerik.Web.UI.GridDataTableFromEnumerable.FillData() +855
   Telerik.Web.UI.GridResolveEnumerable.EnsureInitialized() +29
   Telerik.Web.UI.GridEnumerableFromDataView..ctor(GridTableView owner, DataView dataView, Boolean autoGenerateColumns, GridColumnCollection presentColumns, String[] additionalField, Boolean retrieveAllFields, Boolean enableSplitHeaderText) +260
   Telerik.Web.UI.GridDataSourceHelper.CreateGridEnumerable(GridTableView owner, IEnumerable enumerable, Boolean caseSensitive, Boolean autoGenerateColumns, GridColumnCollection presentColumns, String[] additionalField, Boolean retrieveAllFields, Boolean enableSplitHeaderText) +132
   Telerik.Web.UI.GridTableView.get_ResolvedDataSource() +418
   Telerik.Web.UI.GridTableView.CreateChildControls(IEnumerable dataSource, Boolean useDataSource) +255
   System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +95
   System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +222
   Telerik.Web.UI.GridTableView.PerformSelect() +244
   Telerik.Web.UI.GridTableView.DataBind() +620
   Telerik.Web.UI.RadGrid.OnBubbleEvent(Object source, EventArgs e) +147
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +84
   Telerik.Web.UI.GridItem.OnBubbleEvent(Object source, EventArgs e) +50
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +84
   Telerik.Web.UI.GridItem.OnBubbleEvent(Object source, EventArgs e) +102
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +84
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3804


Here is the error I get with EnableLinqExpressions = False
[IndexOutOfRangeException: Cannot find column Dynamic Column.]
   System.Data.DataTable.ParseSortString(String sortString) +6823231
   System.Data.DataView.set_Sort(String value) +181
   Telerik.Web.UI.GridEnumerableFromDataView.PerformTransformation() +3515
   Telerik.Web.UI.GridEnumerableFromDataView.TransformEnumerable() +24
   Telerik.Web.UI.GridTableView.GetEnumerator(Boolean useDataSource, GridEnumerableBase resolvedDataSource, ArrayList dataKeysArray, Boolean shouldClearDataKeys) +199
   Telerik.Web.UI.GridTableView.CreateControlHierarchy(Boolean useDataSource) +851
   Telerik.Web.UI.GridTableView.CreateChildControls(IEnumerable dataSource, Boolean useDataSource) +1049
   System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +95
   System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +222
   Telerik.Web.UI.GridTableView.PerformSelect() +244
   Telerik.Web.UI.GridTableView.DataBind() +620
   Telerik.Web.UI.RadGrid.OnBubbleEvent(Object source, EventArgs e) +147
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +84
   Telerik.Web.UI.GridItem.OnBubbleEvent(Object source, EventArgs e) +50
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +84
   Telerik.Web.UI.GridItem.OnBubbleEvent(Object source, EventArgs e) +102
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +84
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3804
0
Kostadin
Telerik team
answered on 14 May 2014, 08:09 AM
Hello Billy,

Could you please provide your code declaration and the related code behind in order to investigate the issue further? Also please make sure you are adding the columns dynamically no later than Page_Load event handler. Additionally I prepared a small sample based on the provided information and on my side seems to work correctly.

Regards,
Kostadin
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
Billy
Top achievements
Rank 2
Answers by
Princy
Top achievements
Rank 2
Billy
Top achievements
Rank 2
Kostadin
Telerik team
Share this question
or