QueryableExtensions.ToDataSourceResult throws IndexOutOfRangeException if a DataTable contains deleted rows.
using System.Data;
using Telerik.DataSource;
using Telerik.DataSource.Extensions;
var dt = GetData();
var res = dt.ToDataSourceResult(new DataSourceRequest());
DataTable GetData()
{
DataTable table = new DataTable();
// Make sure you specify the correct types
// If you have any nulls in your (database) Column, you should use nullable type (int?, DateTime?,...)
table.Columns.Add("ProductId", typeof(int));
table.Columns.Add("ProductName", typeof(string));
table.Columns.Add("UnitPrice", typeof(decimal));
table.Columns.Add("UnitsInStock", typeof(short));
// You should set default values to the columns that could be skipped in Create/Edit.
// The alternative approach is to set the default values in the UpdateHandler and/or CreateHandler.
// Having a default value in this case will prevent having DBNull values in your DataTable, because
// DBNull is not parsable to any other primitive type and if you pass it to the grid, some of the operations could be broken
table.Columns["ProductName"].DefaultValue = default(string);
table.Columns["UnitPrice"].DefaultValue = default(decimal);
table.Columns["UnitsInStock"].DefaultValue = default(short);
for (int i = 1; i < 50; i++)
{
table.Rows.Add(i, $"Product{i}", i * 5.2, i * 2);
}
table.AcceptChanges();
var lastRow = table.Rows[table.Rows.Count - 1];
lastRow.Delete();
return table;
}