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

Programmatically handle cell value changes

4 Answers 830 Views
GridView
This is a migrated thread and some comments may be shown as answers.
pierre-jean
Top achievements
Rank 1
Veteran
Iron
pierre-jean asked on 28 Jun 2020, 11:36 AM

Hello

I have a grid that is bound to a view of my SQLdatabase (using Entity frame work)
In order to process a change in a cell value I can not use "context.savechanges" since the Binding is to a Query (view) and not a table
I have Search in two directions:
1. Discard the pending changes in the context and write a function to apply the cell change 
but I have not found a way to discard the changes in the context

2. the other direction I have searched is  to get the changes before the value changed event is fired and process it through my function
But here also I have not managed to make it work

I hope this is understandable ...

 

Thanks in advance for any suggestion

Pierre-Jean

4 Answers, 1 is accepted

Sort by
0
Hristo
Telerik team
answered on 01 Jul 2020, 02:59 PM

Hello Pierre-Jean,

We have a tutorial demonstrating how the grid can be data-bound to Entity Framework using database-first approach: https://docs.telerik.com/devtools/winforms/controls/gridview/populating-with-data/binding-to-entityframework-using-database-first-approach. For code first example you can check this KB article: https://www.telerik.com/support/kb/winforms/details/binding-radgridview-to-entity-framework-using-code-first-approach.

As for the grid, it would not matter what approach will be used and if you will actually connect to a real table or a view in SQL. If I am not mistaken the view should also generate an entity which you can use in your project. I found a good article discussing a similar question: https://www.mssqltips.com/sqlservertip/1990/how-to-use-sql-server-views-with-the-entity-framework/.

I am not sure how discarding the changes in the context would help, however you can check the following threads on StackOverflow: 

One of the main benefits of Entity Framework is that it adds a level of abstraction over the database and you manage it using C# code. If you will not be using the DbContext you may consider changing how you connect to the database, e.g. ADO.NET, SQL readers, etc. You can check the Popuplating with Data section of our documentation for more details and examples: https://docs.telerik.com/devtools/winforms/controls/gridview/populating-with-data/databinding-radgridview.

About your second question, you can use the ValueChanging event. More information on the cycle of the editing events is available here: https://docs.telerik.com/devtools/winforms/controls/gridview/editors/events.

I hope this will help.

Regards,


Hristo
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
0
pierre-jean
Top achievements
Rank 1
Veteran
Iron
answered on 02 Jul 2020, 02:36 PM

Hello Hristo

Thanks for having take the time to look into my problem.

I am afraid that I have not explained clearly the problem I am trying to solve:

1. I have no problement creating an EF connected to my database

2. I have no problem creating a SQL View on a table of my database and integrating this view into my EF

3. I have no problem binding either a table or the view to a datagrid.

The problem comes when I want to manage and store a change made to a cell of the grid.

If the grid is bound to a table, using  in the cellvaluechanged event and context.savechanges works perfectly 
But when the grid is bound to a view, the same context.savechanges generates an error indicating that the operation is not allowed on this entity (I conclude that the view is kind of readonly in the EF).

I have thought of two ways to avoid the error:

1. Discard the pending changes in the EF, but this seems rather complex

2. Use a different mecanism that the cellvalue changed event to avoid the change to be "stored" by the EF and store the change in the database through a specific function. But I have not found how to do this.

I hope I have been more comprehensible 

Thanks a lot

Pierre-Jean

0
Hristo
Telerik team
answered on 03 Jul 2020, 07:29 AM

Hi Pierre-Jean,

I understand your questions and I would like to help you as much as I can. In my personal experience saving the changes to the context has worked fine. Please note, however, that I`m no expert in EF when using views, so I might be missing something. I would like to also point that thеse questions are not related to RadGridView. The grid has no control over the underlying data source and it is up to you to decide how to persist the changes to the database. In this respect StackOverflow seems more suitable for questions about Entity Framework working with views.

As for the grid, if you can handle the ValueChanging event to be notified for a cell`s value change before actually executing that change.

Let me know if you have other questions.

Regards,
Hristo
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
0
pierre-jean
Top achievements
Rank 1
Veteran
Iron
answered on 08 Jul 2020, 02:44 PM

Hello Hirsto

Thank you for your reply I do understand that this question is somewhat out of the scope of Telerik's components,
I will investigate using the value changing event to adress my issue.

Thanks again for the time spent

Best Regards

Pierre-Jean

 

Tags
GridView
Asked by
pierre-jean
Top achievements
Rank 1
Veteran
Iron
Answers by
Hristo
Telerik team
pierre-jean
Top achievements
Rank 1
Veteran
Iron
Share this question
or