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

Grouping on Complex Expressions

2 Answers 98 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Brian Mains
Top achievements
Rank 1
Brian Mains asked on 25 Apr 2012, 01:50 PM
Hello,

In a RadGrid, I can have a column definition like the following:
<asp:BoundColumn .. DataField="County.CountyName" />

But I try this in a grid group by expression:
<tel:GridGroupByExpression DataField="County.CountyName" />
But this throws an exception.  Is there a way I can utilize these complex expressions for grouping?

Thanks.

2 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 25 Apr 2012, 03:42 PM
Hi Brian Mains,

I am not quite sure why you are using Asp BoundColumn. You cant add ASP Bound Column into RadGrid's column collection. I have tried Grouping with complex expression and it is working for me. Please take a look into following code snippet. And make sure that you have set RadGrid's EnableLinqExpressions property as false.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource"
    EnableLinqExpressions="false">
    <MasterTableView>
        <GroupByExpressions>
            <telerik:GridGroupByExpression>
                <GroupByFields>
                    <telerik:GridGroupByField FieldName="Stats.Downloads" />
                </GroupByFields>
                <SelectFields>
                    <telerik:GridGroupByField FieldName="Stats.Downloads" />
                </SelectFields>
            </telerik:GridGroupByExpression>
        </GroupByExpressions>
        <Columns>
            <telerik:GridBoundColumn UniqueName="Id" DataType="System.Int32" DataField="Id" HeaderText="Id">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn UniqueName="Name" DataType="System.String" DataField="Name"
                HeaderText="Name">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn UniqueName="Downloads" DataType="System.Int32" DataField="Stats.Downloads"
                HeaderText="Downloads" DataFormatString="{0:n0}">
            </telerik:GridBoundColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

C#:
public ArrayList DataSource
{
    get
    {
        object obj = Session["DataSource"];
        if (obj == null)
        {
            return new ArrayList();
        }
        return (ArrayList)obj;
    }
    set
    {
        Session["DataSource"] = value;
    }
}
 
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        ArrayList dataSource = new ArrayList();
        Statistics st = new Statistics();
        st.Downloads = 2;
        dataSource.Add(new Product() { Id = 1, Name = "aaa", Stats = st });
        dataSource.Add(new Product() { Id = 2, Name = "bbb", });
        this.DataSource = dataSource;
    }
}
 
protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
    this.RadGrid1.DataSource = this.DataSource;
}
 
public class Product
{
 
    public int Id { get; set; }
    public string Name { get; set; }
    public Statistics Stats { get; set; }
}
 
public class Statistics
{
    public int Downloads { get; set; }
}

Thanks,
Shinu.
0
Brian Mains
Top achievements
Rank 1
answered on 25 Apr 2012, 03:48 PM
Shinu,

Thanks very much.  I mistyped; I should have copied from my solution, as I do have Telerik's GridBoundColumn, not ASP.NET Bound column.  I have what you have, but for me it is complaining that "Stats.Downloads" is not available, in my scenario.  I'll have to look into it more then, it's good to know that it works.

Thanks.
Tags
Grid
Asked by
Brian Mains
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Brian Mains
Top achievements
Rank 1
Share this question
or