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

[Solved] Binding to a generic collection

2 Answers 105 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Flame Boy
Top achievements
Rank 1
Flame Boy asked on 12 Nov 2010, 05:11 AM
Hello,

I am trying to create a reusable web control by encapsulating your grid in my ViewUserControl. This would require being able to bind a collection to the grid without knowing the type at compile time. Is this possible?

Something like this:

Html.Telerik().Grid(Model.GridModel.AsEnumerable())
        .Name(Model.Name)
        .Sortable()
        .Filterable()
        .Scrollable(s => s.Height(150))

Where Model.GridModel is an array of objects with unknown types and all columns are shown. Is this possible? I am willing to use Reflection if you could point me in the right direction.

Thanks!

2 Answers, 1 is accepted

Sort by
0
Flame Boy
Top achievements
Rank 1
answered on 17 Nov 2010, 04:22 AM
Hello,

I have tried to make some progress on this, and I was hoping someone would have some advice for me. I have the Control.ascx which is supposed to contain the grid and the MainView.aspx that will contain a number of Controls. I have added the following member in my model used by Control.ascx:

public Action<object> GridRender { get; set; }

In my MainView.aspx I have:

<%
    Args args = new Args(){ Name = "DataSet1",
        GridRender = new Action<object>(delegate(object o) {
             
            Html.Telerik().Grid<DataSet1Result>(o as DataSet1Result)
                        .Columns(s =>
                        {
                            s.Add(c => c.First);
                            s.Add(c => c.Second);
                            s.Add(c => c.Third);
                        })
        .Name("DataSet1")
        .Sortable()
        .Filterable()
        .Scrollable(s => s.Height(150)).Render();
        })
};
 
    Html.RenderAction("GetReport", "CRM", args);
     
    %>

So I am setting up my Model with a delegate that renders the strongly typed Grid, and then I am calling an ActionResult method that returns my Control.ascx with the Model containing the DataSet1Result as an object (Model.GridModel). The Control.ascx then calls the GridRender method like so:

<%
    Model.GridRender(Model.GridModel);
%>

This approach seemed to work, however it only appears to render the grid with the columns - but no data. I'm not sure what's wrong. When the object gets passed into Html.Telerik().Grid it is strongly typed, and the columns work fine. I don't understand why my data is not showing.

Any help would be much appreciated.
0
Flame Boy
Top achievements
Rank 1
answered on 20 Nov 2010, 06:50 AM
Could someone please just tell me if this is not possible so I can stop wasting my time :-(
Tags
Grid
Asked by
Flame Boy
Top achievements
Rank 1
Answers by
Flame Boy
Top achievements
Rank 1
Share this question
or