Hello. I have the following xamDataGrid:
<igWPF:XamDataGrid Name="SecurityGrid" GroupByAreaLocation="None" DataSource="{Binding UserInfoData, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<igWPF:XamDataGrid.FieldLayoutSettings><igWPF:FieldLayoutSettings HighlightAlternateRecords="True" AutoGenerateFields="False" AllowAddNew="True" AddNewRecordLocation="OnTopFixed"/></igWPF:XamDataGrid.FieldLayoutSettings>
<igWPF:XamDataGrid.Resources><ContextMenu x:Key="RecordContextMenu">
<MenuItem Header="Delete User" Name="DeleteRecord"Command="{Binding DataPresenter.DataContext.DeleteUserommand}"CommandParameter="{Binding RelativeSource={RelativeSource Self},Path=Parent.PlacementTarget.Record}"></MenuItem>
</ContextMenu>
</igWPF:XamDataGrid.Resources>
</igWPF:XamDataGrid>
ViewModel like so
private ObservableCollection<UserInfo> _userInfoData;
public ObservableCollection<UserInfo> UserInfoData {get { return _userInfoData; }set { Set(() => UserInfoData, ref _userInfoData, value); }}
private void OnDeleteUser(DataRecord record){
var index = record.Index;
UserInfoData.RemoveAt(index)
}
When I attempt to delete a record BEFORE I add a record using the build in control activated by AllowAddNew, the index points to the appropriate record I right clicked on. When I add a new record then try to delete that record the record underneath is deleted. The index passes back is the correct index on the grid but points to the wrong record in the collection. The strange thing is that when I save change to the database the newly created record shows up.
Why is the index off after a new record is added?
Hello,
Thank you for your post. I have been looking into it, but I wasn’t able to reproduce your behavior, since I cannot be completely sure how your data is organized and how you delete the records, so could you please send an isolated sample project, where the issue is reproduced, so I can investigate it further for you.
Looking forward for your reply.
Thanks for getting back to me. I've attached a sample project as request. I looked further into this and what appears to be happening is this:
When AddNewRecordLocation is set to OnTopFixed and a new record is added, that record shows up on top in the XamDataGrid and if I inspect my data structure I can see that the count has changed. But that new row has been appended to the data structure so it's the last record in the data structure not the first. You can see that in my sample project. So if I add a record and then delete a record at index 0, the record I've just added is not deleted but original record at index 0. Is there a way around this? Am I not using the control correctly? Thanks.
Hello Kris,
The behavior you described is the designed behavior and no matter where the AddNewRecord is, the record is added at the end of the source (Add method is performed). The thing you can do is log a product idea so when the AddNewRecord is on top an Insert method could be performed and the record could be added at 0 index in the source collection. You can log the product idea here:
http://ideas.infragistics.com/
Steps to create your idea: 1. Log into the Infragistics Product Idea site at http://ideas.infragistics.com (creating a new login if needed).2. Navigate to the product / platform channel of your choice (e.g. WPF, Windows Forms, ASP.NET, HTML5 / Ignite UI, iOS / NucliOS, etc.)3. Add your product idea and be sure to be specific and provide as much detail as possible. Explain the context in which a feature would be used, why it is needed, why it can’t be accomplished today, and who would benefit from it. You can even add screenshots to build a stronger case. Remember that for your suggestion to be successful, you need other members of the community to vote for it. Be convincing!4. “Include a link to this thread” in your idea so product management will be able to look back at this case.
The Product Idea site puts you in the driver’s seat and allows you to track the progress of your ideas at any time, see how many votes it got, read comments from other developers in the community, and see if someone from the product team has additional questions for you.
Also the XamDataGrid has a built in functionality for deleting Records. If you select a Record and press "Delete" key the record will be deleted. You can perform this by code, by calling the XamDataGrid's ExecuteCommand method and use a DataPresenterCommands.DeleteSelectedDataRecord as parameter.
I guess another way 'around this' is to place the add new record control on the bottom of the grid rather than the top which works for me in my scenario.
Thanks for getting back to me.
Referencing the sample project, if you sort by FirstName then press the delete first how button the first row is not deleted but the record that was in the first row originally. The index is not maintained. Is this normal behavior as well? Is there anyway to get around that by not using code behind? I'm using the MVVM patther.
Thanks
Yes, you can put the AddNewRecord at the bottom as well.