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

How to refresh cell value?

14 Answers 1477 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Oleg
Top achievements
Rank 1
Oleg asked on 29 Jun 2010, 12:16 AM
I have a grid with two columns, one of which is editable and another is readonly, set programmatically based on first column value:
                    <t:RadGridView x:Name="NetworkLocationGroupsGridView" CanUserFreezeColumns="False" ShowGroupPanel="False" ItemsSource="{Binding NetworkLocations}" AutoGenerateColumns="False" ShowInsertRow="True" IsFilteringAllowed="False" CanUserReorderColumns="False" RowIndicatorVisibility="Collapsed" CellValidating="NetworkLocationGroupsGridViewCellValidating" CellEditEnded="NetworkLocationGroupsGridView_CellEditEnded" EditTriggers="CellClick">  
                        <t:RadGridView.Columns> 
                            <t:GridViewDataColumn DataMemberBinding="{Binding Id}" Header="Id" Width="50" /> 
                            <t:GridViewDataColumn DataMemberBinding="{Binding LongName}" Header="Name" Width="340" IsReadOnly="True" /> 
                        </t:RadGridView.Columns> 
                    </t:RadGridView> 

In CellValidating event handler, when first column value changes, I calculate and set the value of the second column:
                int id = (int)e.NewValue;  
                var dataTable = dataAccess.GetDataTableFromStoredProcedure("ManagementGetLocationDetail", new Dictionary<string, object> { { "networkLocationId", id } });  
                ((GridViewCell)e.Row.Cells[1]).Value = dataTable.Rows[0]["LongName"].ToString();  
                e.IsValid = true;  
 
or
                int id = (int)e.NewValue;  
                var dataTable = dataAccess.GetDataTableFromStoredProcedure("ManagementGetLocationDetail", new Dictionary<string, object> { { "networkLocationId", id } });  
                var networkLocation = (NetworkLocation)e.Row.Item;  
                networkLocation.LongName = dataTable.Rows[0]["LongName"].ToString();  
                e.IsValid = true;  
 

But even though the second cell value changes, the visual representation stays unchanged, the grid still displays the original cell value. I need the second cell to visually change immediately after first cell passes validation, but I can't find any suitable methods or events to do that. I've tried e.GridViewDataControl.Items.Refresh() but it doesn't work.

14 Answers, 1 is accepted

Sort by
0
Nedyalko Nikolov
Telerik team
answered on 29 Jun 2010, 03:13 PM
Hi Oleg,

All you have to do is to implement INotifyPropertyChanged on your business object (NetworkLocation).
Let me know if this does not help.

Sincerely yours,
Nedyalko Nikolov
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
0
Oleg
Top achievements
Rank 1
answered on 29 Jun 2010, 05:19 PM
Is there any way to manully notify grid view that the cell value has changed? I would very much like to avoid modifying business object to implement presentation-specific interface.
0
Milan
Telerik team
answered on 29 Jun 2010, 05:28 PM
Hi Oleg,

You can always use the Rebind method of RadGridView to refresh the whole grid but that is usually a very costly operation.

Using  INotifyPropertyChanged is the preferred approach. Moreover INotifyPropertyChanged is one of the cornerstones of WPF/Silverlight application development. 


Greetings,
Milan
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
0
Oleg
Top achievements
Rank 1
answered on 29 Jun 2010, 05:32 PM
The grid is small so I'm fine with performance hit. What is the right place to call Rebind? I've tried calling it from CellEditEnded event, but that causes stack overflow.

 

0
Milan
Telerik team
answered on 29 Jun 2010, 05:47 PM
Hi Oleg,

Ups, I have misled you here. Calling Rebind will not help in this situation. Sorry for that. 

Using INotifyPropertyChange would be the way to do it.


Sincerely yours,
Milan
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
0
Ujjwal Lahoti
Top achievements
Rank 1
answered on 30 Jun 2010, 11:53 AM
I also similar concern. Can you provide or point us to an example which uses INotifyPropertyChanged to update the Grid?

Thanks,

0
Maya
Telerik team
answered on 30 Jun 2010, 02:17 PM
Hi Ujjwal Lahoti,

 
You can find a good example for using INotifyPropertyChanged in this blog post. Its main idea is to represent a way for creating a calculated column, but still if you test the application, you will notice that the value in the last column is changed as soon as the value of some of the previous two is also modified. That immediate change in the calculated column is all because of the implementation of the INotifyPropertyChanged interface. 

Regards,
Maya
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
0
Herr
Top achievements
Rank 2
answered on 20 Sep 2010, 07:43 PM
I've got a similar problem:
There are situations where you can't access your business object or better a certain property of the bo, i.e. at cell_drop event.
In this event I have to call cell.Value = getDragDropData() but cell.value doesn't change anything.
So, how can I set the value of a specific cell?

Thanks a lot!

Michael
0
Maya
Telerik team
answered on 21 Sep 2010, 11:03 AM
Hello Michael,

Basically, you can set a value of a particular cell, using this line of code:

cell.ParentRow.Item.GetType().GetProperty(cell.DataColumn.DataMemberBinding.Path.Path).SetValue(cell.ParentRow.Item, newCellValue, null);
 
You may take a look at this forum thread for further reference. 
However, if this does not meet your requirements, I would need a bit more information and specifics about your exact scenario so that I could provide you with a more appropriate solution.

Best wishes,
Maya
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
0
Herr
Top achievements
Rank 2
answered on 27 Sep 2010, 02:00 PM
Hi Maya,

yes, that worked. Using Reflection most likely provides a workaround for everything :-)
...but what's the issue with "cell.Value =..."? Why doesn't that work?
0
Maya
Telerik team
answered on 29 Sep 2010, 07:58 AM
Hello Michael,

The key factor behind the fact "cell.Value" does not work is that we need to think about the underlaying data, but not the UI Element. Thus, in order to update a value, it is recommended to work directly with the item the row is bound to and the properties exposed. 
Furthermore, it is not quite clear what should be the supported scenario when setting a value using the UI element - cell. What if there is a CellTemplate/CellEditTemplate defined ? What should be the value of the cell then ? Thus, as the possible scenarios may be quite different, there is not any straightforward logic that can be implemented.
 

Best wishes,
Maya
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
0
payal
Top achievements
Rank 1
answered on 19 May 2016, 01:19 PM

Dear Maya,

I am trying to implement the same. I want to edit the cell content programmatically.My piece of code is below. I am getting nullreferenceexception

 Cannot convert method group 'GetType' to non-delegate type 'object'. Did you intend to invoke the method? 

Would you please help me with the same.

public static GridViewCell currentCell;
var dataControl = (GridViewDataControl)sender;
currentCell = dataControl.CurrentCell as GridViewCell;
 
   currentCell.ParentRow.Item.GetType().GetProperty(currentCell.DataColumn.DataMemberBinding.Path.Path).SetValue(currentCell.ParentRow.Item, s,null); //s is the string by which I want to replace cell content

 

0
payal
Top achievements
Rank 1
answered on 19 May 2016, 01:33 PM

Dear Maya,

I am trying to implement the same. I want to edit the cell content programmatically.My piece of code is below. I am getting nullreferenceexception

 Cannot convert method group 'GetType' to non-delegate type 'object'. Did you intend to invoke the method?

Would you please help me with the same.

public static GridViewCell currentCell;
 
var dataControl = (GridViewDataControl)sender;
 
currentCell = dataControl.CurrentCell as GridViewCell;
 
currentCell.ParentRow.Item.GetType().GetProperty(currentCell.DataColumn.DataMemberBinding.Path.Path).SetValue(currentCell.ParentRow.Item, s,null); //s is the string by which I want to replace cell content

0
Maya
Telerik team
answered on 20 May 2016, 08:16 AM
Hi Payal,

Generally, the exception you get depends on your exact implementation of the models (the business object used for the source of the grid). 
I prepared a small project based on your code snippet you provided. Please take a look at it and let me know in case you need further assistance. 

Regards,
Maya
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Tags
GridView
Asked by
Oleg
Top achievements
Rank 1
Answers by
Nedyalko Nikolov
Telerik team
Oleg
Top achievements
Rank 1
Milan
Telerik team
Ujjwal Lahoti
Top achievements
Rank 1
Maya
Telerik team
Herr
Top achievements
Rank 2
payal
Top achievements
Rank 1
Share this question
or