I have a grid bound to a collection (ListCollectionView). I'm trying to add new item to the list by
myList.Insert(0, viewModel); myList.MoveCurrentToPosition(0);
Grid has many columns and one is called "Name". It's a text field column with CellValuePresenter displaying text and background using some color.
Now, when I add new item to collection (when user presses "Add" button), I'd like to enter edit mode for Name field right away, so user can edit name without needing to double click on the fields first. The problem is that grid won't go into edit mode. I'm trying to enter edit mode using:
dataGrid.ActiveRecord = dataGrid.Records[0]; // record is added on top dataGrid.ActiveCell = (dataGrid.Records[0] as DataRecord).Cells["Name"]; CellValuePresenter cvp = CellValuePresenter.FromCell(dataGrid.ActiveCell); if (cvp != null) cvp.StartEditMode();But, cvp returned is always null. I found out, that null is returned only for this newly added row, but works great if I use any cell from any other row. Is it because CellValuePresenter has not been created or initialized yet at this point?
Is there another way to start edit mode?
best regards
T
Hi there...
Firstly, rather than using dataGrid.Records[0] to get the new row, I'd be inclined to use dataGrid.RecordManager.CurrentAddRecord.
From that, you can set the active cell, and then enter edit mode like this:
dataGrid.RecordManager.CurrentAddRecord.Cells["Name"].IsActive = true;dataGrid.ExecuteCommand(DataPresenterCommands.StartEditMode);
Hi Phil,
The RecordManager.CurrentAddRecord is used for the UI AddNewRow functionality of the grid. In your case you are adding the records to the collection using a button so this is the reason why the code below will not work at all:
dataGrid.RecordManager.CurrentAddRecord.Cells["Name"].IsActive = true;
You can try using the XamDataGrid.Records collection ,cast the first records to DataRecord to access the Cells collection and then set IsActive to the cell you want or just set the ActiveCell. This is the recommendation to execute the EnterEditMode command. About your first approach the CellValuePresenter of the cell is null because of the virtualization techniques that the grid is using for better performance. I suppose you are probably accessing a cell that is out of view. In order to resolve this you have to make sure the cell is in view (scroll to the cell). Another reason causing the EnterEditMode command not to execute is setting the AllowEdit mode of the grid to false. Let me know if this is true and if you can resolve the issue with the following code:
(dataGrid.Records[0] as DataRecord).Cells["Name"].IsActive = true;dataGrid.ExecuteCommand(DataPresenterCommands.StartEditMode);
-Vlad