Unplanned
Last Updated: 11 Nov 2024 14:16 by ADMIN
John
Created on: 11 Nov 2024 14:01
Category: Grid
Type: Bug Report
1
No property or field 'Name' exists in type 'DataRowView'

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:

  1. Bind the Grid to an empty data source at initial load
  2. Filter any column
  3. Insert a new record

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);
}

1 comment
ADMIN
Attila Antal
Posted on: 11 Nov 2024 14:16

Hello John,

Thank you for reporting the issue.

Workaround

Until the problem is fixed, we can suggest canceling the Filtering action in the ItemCommand event.

protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName == RadGrid.FilterCommandName && SessionDataSource.Rows.Count < 1)
    {
        e.Canceled = true; // cancel the filtering
        // Send a message to the user saying that there are no records to Filter
    }
}

 

Regards,
Attila Antal
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages! Or perhaps, if you are new to our Telerik family, check out our getting started resources