简体   繁体   中英

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"}

    };
}

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM