Hi
I have customised the XamDataGrid to add new row on click of a button outside the grid.
For this purpose i bind an observable collection to the grid.
The code in custom XamDataGrid to add a new row is as below
Public Sub AddRow(ByVal tempEntity As Object)
Try
Dim dataSource As IList = Me
.DataSource
dataSource.Add(tempEntity)
Me.DataSource = dataSource
Me.ExecuteCommand(DataPresenterCommands.RecordLastOverall)
intNewRowCounter = intNewRowCounter + 1
Me.ActiveRecord = Me
.Records(dataSource.Count - 1)
lstSelectedItems = New List(Of Object)
lstSelectedItems.Add(TryCast(Me.ActiveRecord, DataRecord).DataItem)
intPrevIndex = TryCast(MeEnableCellEditing().ActiveRecord, DataRecord).DataItemIndex
Me.FieldSettings.LabelClickAction = LabelClickAction.Nothing
RaiseEvent OnAdd(Me)
Catch ex As Exception
Throw ex
End Try
End Sub
Here the tempEntity is passed to the grid and is added to the Datasorce of the grid.
in this line of code
Me.ActiveRecord = Me.Records(dataSource.Count - 1)
i am setting the newly added row as the active one
This is working fine in the prototype i developed using trial cersion 8.2
but recently i bought the full version V 9.1 and this code is throwing error at the line above
The reason being Me.Records is not updated with the new count while the datasource is.
The exception was Index out of bounds.
The only thing not common to the development environment was the version.
could this be a bug.
Expecting a reply
Thanks in advance
Abhi
Hello Abhi,
Here is the code I am using to reproduce this, but I was not able:
ObservableCollection<Person> dataSource = xamDataGrid1.DataSource as ObservableCollection<Person>;
Person p = new Person();
p.FName = "New First Name";
p.LName = "New Last Name";
dataSource.Add(p);
xamDataGrid1.DataSource = dataSource;
xamDataGrid1.ExecuteCommand(DataPresenterCommands.RecordLastOverall);
counter++;
xamDataGrid1.ActiveRecord = xamDataGrid1.Records[dataSource.Count -1];
lstSelectedItems = new List<Person>();
lstSelectedItems.Add((xamDataGrid1.ActiveRecord as DataRecord).DataItem as Person);
prevIndex = (xamDataGrid1.ActiveRecord as DataRecord).DataItemIndex;
xamDataGrid1.FieldSettings.LabelClickAction = LabelClickAction.Nothing;
I would suggest using the Records' count instead of the count of the items in the collection like this:
xamDataGrid1.ActiveRecord = xamDataGrid1.Records[xamDataGrid1.Records.Count - 1];
You can also try using BindingList<T> instead of ObservableCollection<T>.
If none of these solves the problem, please attach a sample project that reproduces the issue.
Regards,
Alex.