Setting RadGrid data source from client side

2 Answers 48 Views
Grid
Omar
Top achievements
Rank 1
Veteran
Iron
Omar asked on 15 May 2021, 05:55 PM

Hi,

I am trying to set the data source for grid from client side, Below is example of the code.

 

<telerik:RadGrid ID="Grid_geoData" runat="server" AutoGenerateColumns="false" Height="200px" > <MasterTableView DataKeyNames="Text" ClientDataKeyNames="Text" ShowHeadersWhenNoRecords="true" Visible="true"> <Columns> <telerik:GridBoundColumn DataField="Text" HeaderText="Comp. name" UniqueName="comName" /> </Columns> </MasterTableView> </telerik:RadGrid>

 


function pageLoad() {
            var data =
                [
                    { "ID": 1, "Text": "Text1" },
                    { "ID": 2, "Text": "Text2" }
                ];
            var mtv =  $find("<%= Grid_geoData.ClientID %>").get_masterTableView();
            mtv.set_dataSource(data);
            mtv.dataBind();
        }

I get error at line 

mtv.set_dataSource(data);

Do anyone have idea what I am doing wrong?

Regards,

Omar

 

2 Answers, 1 is accepted

Sort by
1
Accepted
Vessy
Telerik team
answered on 19 May 2021, 04:25 PM

Hi Omar,

The masterTableView object is still not created in the moment when you try to bind it to the passed JSON data, thus the gird is not bound.

Passing some empty data source to the Grid in its NeedDataSource server-side event handler should allow you to bind the Grid successfully on the client-side in the pageLoad handler:

    protected void Grid_geoData_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        Grid_geoData.DataSource = new string[] { };
    }

Regards,
Vessy
Progress Telerik

Тhe web is about to get a bit better! 

The Progress Hack-For-Good Challenge has started. Learn how to enter and make the web a worthier place: https://progress-worthyweb.devpost.com.

0
Omar
Top achievements
Rank 1
Veteran
Iron
answered on 20 May 2021, 06:13 AM

Hi,

It solved my problem nicely.

Many thanks for the help.

Regards,

Omar

Vessy
Telerik team
commented on 20 May 2021, 06:21 AM

You are welcome, Omar :) I am glad my reply was helpful.
Tags
Grid
Asked by
Omar
Top achievements
Rank 1
Veteran
Iron
Answers by
Vessy
Telerik team
Omar
Top achievements
Rank 1
Veteran
Iron
Share this question
or