This is a migrated thread and some comments may be shown as answers.

e.Row.DataBoundItem is readonly now

2 Answers 434 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Nick Gebbett
Top achievements
Rank 1
Nick Gebbett asked on 22 Sep 2010, 06:17 PM
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 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 23 Sep 2010, 09:50 PM
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
0
Julian Benkov
Telerik team
answered on 28 Sep 2010, 12:43 PM
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
Tags
GridView
Asked by
Nick Gebbett
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Julian Benkov
Telerik team
Share this question
or