How to refresh cell value?

15 posts, 0 answers
  1. Oleg
    Oleg avatar
    4 posts
    Member since:
    Jun 2010

    Posted 28 Jun 2010 Link to this post

    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.
  2. Nedyalko Nikolov
    Admin
    Nedyalko Nikolov avatar
    871 posts

    Posted 29 Jun 2010 Link to this post

    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
  3. UI for WPF is Visual Studio 2017 Ready
  4. Oleg
    Oleg avatar
    4 posts
    Member since:
    Jun 2010

    Posted 29 Jun 2010 Link to this post

    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.
  5. Milan
    Admin
    Milan avatar
    1989 posts

    Posted 29 Jun 2010 Link to this post

    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
  6. Oleg
    Oleg avatar
    4 posts
    Member since:
    Jun 2010

    Posted 29 Jun 2010 Link to this post

    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.

     

  7. Milan
    Admin
    Milan avatar
    1989 posts

    Posted 29 Jun 2010 Link to this post

    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
  8. Ujjwal Lahoti
    Ujjwal Lahoti avatar
    67 posts
    Member since:
    Jan 2010

    Posted 30 Jun 2010 Link to this post

    I also similar concern. Can you provide or point us to an example which uses INotifyPropertyChanged to update the Grid?

    Thanks,

  9. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 30 Jun 2010 Link to this post

    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
  10. Herr
    Herr avatar
    23 posts
    Member since:
    Feb 2009

    Posted 20 Sep 2010 Link to this post

    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
  11. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 21 Sep 2010 Link to this post

    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
  12. Herr
    Herr avatar
    23 posts
    Member since:
    Feb 2009

    Posted 27 Sep 2010 Link to this post

    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?
  13. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 29 Sep 2010 Link to this post

    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
  14. payal
    payal avatar
    8 posts
    Member since:
    May 2016

    Posted 19 May in reply to Maya Link to this post

    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

     

  15. payal
    payal avatar
    8 posts
    Member since:
    May 2016

    Posted 19 May in reply to Maya Link to this post

    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

  16. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 20 May Link to this post

    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.
Back to Top
UI for WPF is Visual Studio 2017 Ready