<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; |
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
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
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
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
Thanks,
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.
Maya
the Telerik team
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
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
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?
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.
Maya
the Telerik team
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
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
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