Upon inserting a new record after filtering on the Grid with No records, the exception "No property or field 'Name' exists in type 'DataRowView'" is thrown.
Steps to reproduce:
Code to replicate the issue:
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" AllowFilteringByColumn="true"
OnNeedDataSource="RadGrid1_NeedDataSource" OnInsertCommand="RadGrid1_InsertCommand">
<MasterTableView CommandItemDisplay="Top" DataKeyNames="ID">
<Columns>
<telerik:GridEditCommandColumn></telerik:GridEditCommandColumn>
<telerik:GridButtonColumn CommandName="Delete"></telerik:GridButtonColumn>
<telerik:GridBoundColumn DataField="ID" HeaderText="ID" UniqueName="ID" ReadOnly="true"
CurrentFilterFunction="Contains" AutoPostBackOnFilter="true" />
<telerik:GridBoundColumn DataField="Name" HeaderText="Name" UniqueName="Name"
AutoPostBackOnFilter="false" />
<telerik:GridBoundColumn DataField="Category" HeaderText="Category" UniqueName="Category" DataType="System.String"
CurrentFilterFunction="Contains" AutoPostBackOnFilter="true" />
<telerik:GridBoundColumn DataField="Price" HeaderText="Price" UniqueName="Price"
CurrentFilterFunction="Contains" AutoPostBackOnFilter="true" DataFormatString="{0:C}" />
</Columns>
<EditFormSettings InsertCaption="Add New Item" />
</MasterTableView>
</telerik:RadGrid>
C#
private const string DataTableSessionKey = "RadGridDataTable";
private DataTable SessionDataSource
{
get
{
DataTable dt = Session[DataTableSessionKey] as DataTable;
if (dt == null || !IsPostBack)
{
// Create an empty DataTable with schema defined
dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Category", typeof(string));
dt.Columns.Add("Price", typeof(decimal));
dt.PrimaryKey = new DataColumn[] { dt.Columns["ID"] };
Session[DataTableSessionKey] = dt;
}
return dt;
}
}
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
(sender as RadGrid).DataSource = SessionDataSource;
}
protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
{
GridEditableItem insertItem = (GridEditableItem) e.Item;
Hashtable newValues = new Hashtable();
insertItem.ExtractValues(newValues);
DataRow findLastItem = SessionDataSource.Select("ID=MAX(ID)").FirstOrDefault();
newValues["ID"] = findLastItem != null ? (int) findLastItem["ID"] + 1 : 0;
DataRow rowToInsert = SessionDataSource.NewRow();
foreach (DictionaryEntry entry in newValues)
{
rowToInsert[entry.Key.ToString()] = entry.Value ?? DBNull.Value;
}
SessionDataSource.Rows.Add(rowToInsert);
}