How can I sort the contents Telerik GridBoundColumn based upon a substring?

我是研究僧i 提交于 2019-12-24 12:34:36

问题


I have the following:

<telerik:GridBoundColumn HeaderText="Name(s)" UniqueName="colNames" DataField="NameList"
                    AllowSorting="false">

the data being bound to this is coming from a SQL stored procedure that I can't modify without affecting a number of other pages. I need to sort these items by a substring; the field to be displayed is 'Firstname Lastname', and I need to sort on last name. Is there a way to modify the above to sort on the substring after ' '? I know this is an imperfect solution to finding the last name (F. Murray Abraham == 'M') but that's the approach that I'm tasked to implement.

I'm trying to use the Telerik SortExpression but can't get the syntax figured out (assuming that it's even usable for this purpose).

If it helps, the site codebehind is built in VB.NET.


回答1:


The best way is to modify the stored proc by adding new column to the returned dataset which contains the last name, provided the last name column is defined separately from the first name. This won't affect other pages which are already using the stored proc, unless you use AutoGeneratedColumn for the RadGrid on those pages.

If the name column returned from the stored proc is in full format (i.e "John Smith") and/or there is no way you can add field to the returned dataset from the stored proc, you will have to do this in code-behind:

  1. Execute the stored proc and fill a DataTable with the returned dataset
  2. add a new column 'LastName' of type String to the DataTable object
  3. For each row in DataTable, extract the lastname from the "NameList" column
  4. assign it to the 'LastName' column
  5. set the modified DataTable object to your RadGrid.DataSource

Remember to add SortExpression="LastName" to your GridBoundColumn tag above and AllowSorting="true".

To extract the last name:

    If Not IsDBNull(row!NameList) Then
        Dim nameparts() As String = Cstr(row!NameList).Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries)
        If nameparts.Count > 0 Then
            row!lastname = nameparts.GetUpperBound(0)
        End If
    End If


来源:https://stackoverflow.com/questions/15934925/how-can-i-sort-the-contents-telerik-gridboundcolumn-based-upon-a-substring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!