Dynamic Linq Data Source and Columns

1 posts, 0 answers
  1. Tyson
    Tyson avatar
    9 posts
    Member since:
    Apr 2007

    Posted 09 Apr 2009 Link to this post

    So I've been going round and round trying to get Linq to work with RadGrid and be able to both filter and sort my columns (which are added at runtime). I was capturing all kinds of events trying to get the right sequence for determining whether I need to rebuild my columns, bind data, and set the datasource. The closest I got was either I could sort the grid and the column headers would not disappear, or I could filter the columns on the first postback. If sort worked, then I would have to filter twice to get the filter to stick.

    Long story short, if you are binding, through Linq, to an array of some type (guessing anything but a true datasource control), you have to use the AsQueryable() at the end of it. Then, sorts work, filters work, and it doesn't require any more code than to fetch the data and build the grid columns.

    Why oh why didn't I find this sooner :)

    <%@ Page Language="C#" AutoEventWireup="true"   
        CodeFile="Default.aspx.cs" Inherits="Webpage.Default" %> 
    <%@ Register Assembly="Telerik.Web.UI, Version=2008.3.1125.35, Culture=neutral, PublicKeyToken=121fae78165ba3d4" 
        Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml">  
    <head runat="server">  
        <title>Configurable List</title> 
        <form id="form1" runat="server">  
            <telerik:RadScriptManager ID="RadScriptManager1" runat="server">  
            <telerik:RadGrid ID="gridResults" runat="server" GridLines="None"   
                AllowFilteringByColumn="True" AllowPaging="True" AllowSorting="True">  
                <HeaderContextMenu EnableTheming="True">  
                    <CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation> 
                <MasterTableView AutoGenerateColumns="False">  
                        <HeaderStyle Width="20px"></HeaderStyle> 
                        <HeaderStyle Width="20px"></HeaderStyle> 
                    <Scrolling AllowScroll="True" UseStaticHeaders="True" /> 
                <FilterMenu EnableTheming="True">  
                    <CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation> 

    using System;  
    using System.Configuration;  
    using System.Data;  
    using System.Linq;  
    using System.Web;  
    using System.Web.Security;  
    using System.Web.UI;  
    using System.Web.UI.HtmlControls;  
    using System.Web.UI.WebControls;  
    using System.Web.UI.WebControls.WebParts;  
    using System.Xml.Linq;  
    using Entities;  
    using System.Collections.Generic;  
    using System.Linq.Expressions;  
    using Telerik.Web.UI;  
    using System.Linq.Dynamic;  
    namespace Webpage {  
        public partial class Default : System.Web.UI.Page {  
            private ShipmentList _results = new ShipmentList();  
            private string[] _columns = new string[] {  
            protected void Page_Load(object sender, EventArgs e) {  
                var query = _results.Shipments.AsQueryable();  
                gridResults.DataSource = query;  
            private void RebuildGrid() {  
                foreach (string colName in _columns) {  
                    GridBoundColumn gridCol = new GridBoundColumn();  
                    gridCol.DataField = colName;  
                    gridCol.HeaderText = colName;  
                    gridCol.SortExpression = colName;  
                    gridCol.UniqueName = colName;  
            private void RefreshData() {  
                _results = new ShipmentList();  
                List<Shipment> shipments = new List<Shipment>();  
                for (int i = 0; i < 100; i++) {  
                    Shipment shipment = new Shipment();  
                    shipment.AuthNumber = (i + 972234).ToString();  
                    shipment.ArrivalDate = DateTime.Now;  
                    shipment.Destination = "Someplace";  
                    shipment.Origin = "Diffplace";  
                    shipment.ShipmentDate = DateTime.Now;  
                _results.Shipments = shipments;  
Back to Top