e.Row.DataBoundItem is readonly now

3 posts, 0 answers
  1. Nick Gebbett
    Nick Gebbett avatar
    13 posts
    Member since:
    May 2010

    Posted 22 Sep 2010 Link to this post

    I have been using CellValidating event to validate cells data that user enters in a grid. It was important that e.Row.DataBoundItem != null and represent my Entity.
    For this reason I have been using 
    BaseGridView_UserAddedRow event 
    and BaseGridView_DefaultValuesNeeded

    like this

    /// <summary>
    /// Set DataBoundItem when user is adding new row
    /// </summary>
    protected override void BaseGridView_DefaultValuesNeeded(object sender, GridViewRowEventArgs e)
        e.Row.DataBoundItem = new TEntity();
    /// <summary>
    /// Update Grid DataBountItem after saving. If don't do this, ((Entity)DataBoundItem).Id will be 0.
    /// </summary>
    private void BaseGridView_UserAddedRow(object sender, GridViewRowEventArgs e)
        e.Row.DataBoundItem = Entity;

    now in latest build e.Row.DataBoundItem is readonly.
    My questions are:
    1) how to set DataBoundItem before CellValidating?
    2) how to set DataBoundItem after saving data to database in case I save it during BaseGridView_RowValidating event?

  2. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 23 Sep 2010 Link to this post

    Hello Alexander,

    The Row.DataBoundItem should always be readonly when you are working with a bounded collection, because you should handle the custom data operations on your collection directly, not the grid.

    1. In my point of view it might be a better idea to validate data from the cells directly, this way you will be able to cancel the current operation and you won't create always a new instance of that class thus it will be cleaner.

    2. If you are using a binding list, all the changes you are making to your collection will be applied automatically on the grid, so if you have a bounded item and you save / load from the db and there are some changes to that item those changes will be applied automatically.

    Please let me know if there is anything else i can do to help,

    Best Regards,
    Emanuel Varga
  3. Julian Benkov
    Julian Benkov avatar
    1135 posts

    Posted 28 Sep 2010 Link to this post

    Hello Alexander,

    The DataBoundItem should always be a readonly property. The DataBoundItem is used only in the bound mode of RadGridView control and can not be changed directly, this will break the data integrity of the DataSource object.

    In this case you can use Emanuel's suggestion or validate values on the UserAddingRow event and Cancel it if the data is not valid.

    On UserAddedRow, the DataBoundItem is returned from the internal CurrencyManager and you can only change its cell data.

    Thank you Emanuel for the suggestions.

    All the best,

    Julian Benkov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top