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

Dynamic Linq Data Source and Columns

0 Answers 224 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Tyson
Top achievements
Rank 1
Tyson asked on 09 Apr 2009, 04:08 PM
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> 
</head> 
<body> 
    <form id="form1" runat="server">  
    <div> 
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server">  
        </telerik:RadScriptManager> 
          
        <telerik:RadGrid ID="gridResults" runat="server" GridLines="None"   
            AllowFilteringByColumn="True" AllowPaging="True" AllowSorting="True">  
            <HeaderContextMenu EnableTheming="True">  
                <CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation> 
            </HeaderContextMenu> 
            <MasterTableView AutoGenerateColumns="False">  
                <RowIndicatorColumn> 
                    <HeaderStyle Width="20px"></HeaderStyle> 
                </RowIndicatorColumn> 
                <ExpandCollapseColumn> 
                    <HeaderStyle Width="20px"></HeaderStyle> 
                </ExpandCollapseColumn> 
                <Columns> 
                </Columns> 
            </MasterTableView> 
            <ClientSettings> 
                <Scrolling AllowScroll="True" UseStaticHeaders="True" /> 
            </ClientSettings> 
            <FilterMenu EnableTheming="True">  
                <CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation> 
            </FilterMenu> 
        </telerik:RadGrid> 
          
    </div> 
    </form> 
</body> 
</html> 
 

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[] {  
            "AuthNumber""Origin""Destination""ShipmentDate""ArrivalDate" 
        };  
 
        protected void Page_Load(object sender, EventArgs e) {  
            RefreshData();  
            RebuildGrid();  
 
            var query = _results.Shipments.AsQueryable();  
            gridResults.DataSource = query;  
 
            gridResults.Rebind();  
        }  
 
        private void RebuildGrid() {  
            gridResults.MasterTableView.Columns.Clear();  
 
            foreach (string colName in _columns) {  
                GridBoundColumn gridCol = new GridBoundColumn();  
                gridCol.DataField = colName;  
                gridCol.HeaderText = colName;  
                gridCol.SortExpression = colName;  
                gridCol.UniqueName = colName;  
                gridResults.MasterTableView.Columns.Add(gridCol);  
            }  
        }  
 
        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;  
                shipments.Add(shipment);  
            }  
 
            _results.Shipments = shipments;  
        }  
    }  

No answers yet. Maybe you can help?

Tags
Grid
Asked by
Tyson
Top achievements
Rank 1
Share this question
or