MVC rid - Paste data from excel

2 posts, 0 answers
  1. Mark
    Mark avatar
    1 posts
    Member since:
    May 2017

    Posted 24 Jul 2017 Link to this post

    Is it possible to paste data from Excel and have the create method fire to append each inserted row?

    The example from the documentation (http://docs.telerik.com/kendo-ui/controls/data-management/grid/how-to/excel/copy-from-excel-to-grid) simply replaces the existing grid's datasource whereas I would like to append each row that is pasted in so that it maintains the existing data and adds the newly pasted data.

    My example Grid currently looks like:

    @(Html.Kendo().Grid<InvoiceDetailVM>()
    .Name("MyGrid")
    .Columns(columns =>
    {
    columns.Bound(p => p.ID).Hidden();
    columns.Bound(p => p.Description);
    columns.Command(command => { command.Edit(); command.Destroy(); }).Width(250);
    })

    .Editable(editable => editable.Mode(GridEditMode.InLine))
    .Sortable()

    .DataSource(dataSource => dataSource // Configure the grid data source
    .Ajax()                                                    
    .PageSize(25)
    .Events(events => events.Error("window.KendoGrid_Error"))
    .Model(model =>
    {
    model.Id(i => i.ID); // Specify the property which is the unique identifier of the model.
    model.Field(i => i.ID).Editable(false);
    })
    .Create(update => update.Action("Create", "Test"))
    .Read(read => read.Action("Read", "Test"))
    .Update(update => update.Action("Edit"))
    .Destroy(update => update.Action("Delete", "Test"))
    )                                
    )

    Any help would be appreciated.

    Thanks!

  2. Stefan
    Admin
    Stefan avatar
    2957 posts

    Posted 26 Jul 2017 Link to this post

    Hello Mark,

    The desired result can be achieved by retrieving the current data before adding the new one. The data can be retrieved using the data method of the dataSource:

    https://admin.telerik.com/docs.telerik.com/kendo-ui/api/javascript/data/datasource#methods-data

    // This is the changed line
    var
    data = grid.dataSource.data();
     
               for (var i = 0; i < rows.length; i++) {
                 var cells = rows[i].split('\t');
                 data.push({
                   Name: cells[0],
                   Age: cells[1]
                 });
               };
               grid.dataSource.data(data);

    http://dojo.telerik.com/utENI

    I hope this is helpful.

    Regards,
    Stefan
    Progress Telerik
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top