sakir
sakir

Reputation: 3502

Bind object[] to a datasource in Telerik

I am trying to bind the data to my grid, but it shows me empty rows.

How can I achieve this? Is it not possible to bind object[] to a datasource in Telerik?

<telerik:RadGrid ID="RadGrid1" runat="server">
    <MasterTableView>
        <Columns>
            <telerik:GridBoundColumn UniqueName="ContactTitle"
HeaderText="Bound Column" DataField="ContactTitle">
            </telerik:GridBoundColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

page load event:

protected void Page_Load(object sender, EventArgs e)
{
    RadGrid1.DataSource = BinddataGrid();
}

private object[] BinddataGrid()
{
    return new object[] { 

        new object[] {"TEST1"},
        new object[] {"TEST2"},
        new object[] {"TEST3"},
        new object[] {"TEST4"}

    };
}

Upvotes: 0

Views: 697

Answers (2)

rdmptn
rdmptn

Reputation: 5603

Yes, it is. You may want to use the NeedDataSource event, though: http://demos.telerik.com/aspnet-ajax/grid/examples/data-binding/simple-vs-advanced/defaultcs.aspx

Upvotes: 0

Ali Baghdadi
Ali Baghdadi

Reputation: 648

you are binding the grid with an array of array of objects, instead, you must bind it to an array of objects first, and second, when defining an object, use only new keyword, and third, you are mapping the column to a datafield ContactTitle that does not exist, this code may work:

protected void Page_Load(object sender, EventArgs e)
{
    RadGrid1.DataSource = BinddataGrid();
}

private object[] BinddataGrid()
{
    return new object[] { 

        new  {ContactTitle = "TEST1"},
        new   {ContactTitle ="TEST2"},
        new   {ContactTitle ="TEST3"},
        new   {ContactTitle ="TEST4"}

        };
}

and in your radgrid, add a property AutoGenerateColumns="false":

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false">
    <MasterTableView>
        <Columns>
            <telerik:gridboundcolumn uniquename="ContactTitle" headertext="Bound Column"                 datafield="ContactTitle">
                </telerik:gridboundcolumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

Upvotes: 3

Related Questions