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

how to add column which is updated using code and can be sortable

3 Answers 42 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Lina
Top achievements
Rank 1
Lina asked on 11 May 2014, 11:36 AM
How do I add column which is updated using code and can be sortable?
I have RadGrid, which get the data using Entities Framework query.
I added label (lblTest1) to get additional information which isn't returned by the query, but in this way the column isn't sortable (because it's not one of the query outcomes).
Is there a different way to update the column so it will be sortable using the field data or is there a way to sort according to this label?

aspx:

<telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" CellSpacing="0" 
DataSourceID="ObjectDataSourceIems" OnItemDataBound="RadGrid1_OnItemDataBound" GridLines="None" >
    <MasterTableView DataSourceID="ObjectDataSourceItems" 
        <CommandItemSettings ExportToPdfText="Export to PDF"></CommandItemSettings>
        <RowIndicatorColumn Visible="True" FilterControlAltText="Filter RowIndicator column">
            <HeaderStyle Width="20px"></HeaderStyle>
        </RowIndicatorColumn>
        <ExpandCollapseColumn Visible="True" FilterControlAltText="Filter ExpandColumn column">
            <HeaderStyle Width="20px"></HeaderStyle>
        </ExpandCollapseColumn>
        <Columns>
            <telerik:GridBoundColumn DataField="ItemID" FilterControlAltText="ItemID" HeaderText="ItemID" SortExpression="ItemID" UniqueName="ItemID">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="FirstName" FilterControlAltText="First Name" HeaderText="First Name" SortExpression="FirstName" UniqueName="FirstName">
           </telerik:GridBoundColumn>
            <telerik:GridTemplateColumn HeaderText="Test1" SortExpression="" UniqueName="Test1">                                
                <ItemTemplate>
                    <asp:Label ID="lblTest1" runat="server" Text="" />
                </ItemTemplate>
            </telerik:GridTemplateColumn>    
        </Columns>
        <EditFormSettings>
            <EditColumn FilterControlAltText="Filter EditCommandColumn column"></EditColumn>
        </EditFormSettings>
        <PagerStyle PageSizeControlType="RadComboBox"></PagerStyle>
    </MasterTableView>
    <PagerStyle PageSizeControlType="RadComboBox"></PagerStyle>
    <FilterMenu EnableImageSprites="False"></FilterMenu>
</telerik:RadGrid>

<asp:ObjectDataSource ID="ObjectDataSourceIems" runat="server" SelectMethod="GetItemsPaged"
    SelectCountMethod="GetObjectDataSourceIemsByNameCount"
    TypeName="test.BLL.Items.ItemsRepsitory" EnablePaging="True" SortParameterName="sortExpression">
    <SelectParameters>
        <asp:ControlParameter ControlID="TextBoxSearchString" Name="nameSearchString" PropertyName="Text" Type="String" />
        <asp:ControlParameter ControlID="RadioButtonListColumns" Name="searchColumn" PropertyName="SelectedValue" Type="String" />
    </SelectParameters>
</asp:ObjectDataSource>

cs:
public void RadGrid1_OnItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item.ItemType != GridItemType.Item && e.Item.ItemType != GridItemType.AlternatingItem) return;
    var item = (Item)e.Item.DataItem;

    MoreInfoRepository moreInfoRepository = new MoreInfoRepository();
    MoreInfo moreInfo = moreInfoRepository.GetMoreInfo(item.ItemID);
    if (moreInfo != null)
    {
        var lblTest1 = (Label)e.Item.FindControl("lblTest1");
        lblTest1.Text = moreInfo.Test1;
    }
}

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 12 May 2014, 04:44 AM
Hi Lina,

Sorting functionality for the unbound columns in the RadGrid is not possible. As those columns do not contain data items, the sorting will not work. What you can do is to manually "sort" the values  by using the header click for the unbound columns.
Please take a look at this forum, which discuss about this issue.
http://www.telerik.com/forums/sort-by-unbound-columns#wNXW15XS-kaY8uSB0s4Cow

Thanks,
Princy
0
Lina
Top achievements
Rank 1
answered on 13 May 2014, 06:47 AM
i get an exception because there is a call to the method which perform
the select with sortExpression="Test1 ASC", but there is no such field
there
0
Accepted
Princy
Top achievements
Rank 2
answered on 14 May 2014, 06:36 AM
Hi Lina,

I'm afraid your requirement is not possible. Since you donot have a field "Test ASC", it cannot sort.  Grid items and columns are displayed only as a result of data binding. To enable sorting for GridTemplateColumn, you need to set appropriate SortExpression values for those columns.  The SortExpression value should match the data field you want to sort on.

Thanks,
Princy
Tags
Grid
Asked by
Lina
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Lina
Top achievements
Rank 1
Share this question
or