I have a data grid where certain cells should not be editable.
When the grid first draws, I have three rows of data and some cells can be edited, and some not. Then I have a button where I add a an object to the ObservableCollection populating the list, and in this case sometimes a cell should have its editing turned off.
How do I do this in the code behind?
Thanks.
Mike
Hello Mike,
Thank you for your post. I have been looking into it and I can say that the AllowEdit Property belongs to the Field’s Settings and it is set for all the Cells from this Field, so they are all editable or not. Also I can say that when you add new Record the XamDataGrid’s InitializeRecord event is fired and you can handled it and from the EventArgs you can get the Record, Cells Fields, etc.
Hope this helps you.
Stefan,
Thanks for helping with what message I want to trap. But, how do I prevent editing in a particular cell? My customer wants to turn editing on or off at a particular cell, as some cells in a column may be editable or not based on what is in the row.
So, given a data grid, how do I manage to prevent editing at the cell level?
I have been looking into you requirement and I created a sample project for you with the functionality you want. Basically the whole XamDataGrid is editable, but I handled the EditModeStarting event and canceled it if a certain condition is met. Please let me know if this helps you or you need further assistance on this matter.
Looking forward for your reply.
Thanks for doing this. What I wound up doing yesterday was to trap the CellActivated message and then grabbed the record index and set the AllowEdit property to true or false for the row. I'll look at what you did and was wondering if that is a better way than this:
private void dgExpandedPFT_CellActivated( object sender, Infragistics.Windows.DataPresenter.Events.CellActivatedEventArgs e ) { switch( e.Cell.Record.Index ) { case 0: { this.dgExpandedPFT.Records[ e.Cell.Record.Index ].FieldLayout.Fields[ 3 ].Settings.AllowEdit = false; this.dgExpandedPFT.Records[ e.Cell.Record.Index ].FieldLayout.Fields[ 4 ].Settings.AllowEdit = false; this.dgExpandedPFT.Records[ e.Cell.Record.Index ].FieldLayout.Fields[ 5 ].Settings.AllowEdit = false; this.dgExpandedPFT.Records[ e.Cell.Record.Index ].FieldLayout.Fields[ 6 ].Settings.AllowEdit = true; this.dgExpandedPFT.Records[ e.Cell.Record.Index ].FieldLayout.Fields[ 7 ].Settings.AllowEdit = true; this.dgExpandedPFT.Records[ e.Cell.Record.Index ].FieldLayout.Fields[ 8 ].Settings.AllowEdit = false; this.dgExpandedPFT.Records[ e.Cell.Record.Index ].FieldLayout.Fields[ 9 ].Settings.AllowEdit = false; break; } case 1: { this.dgExpandedPFT.Records[ e.Cell.Record.Index ].FieldLayout.Fields[ 3 ].Settings.AllowEdit = false; this.dgExpandedPFT.Records[ e.Cell.Record.Index ].FieldLayout.Fields[ 4 ].Settings.AllowEdit = false; this.dgExpandedPFT.Records[ e.Cell.Record.Index ].FieldLayout.Fields[ 5 ].Settings.AllowEdit = false; this.dgExpandedPFT.Records[ e.Cell.Record.Index ].FieldLayout.Fields[ 6 ].Settings.AllowEdit = true; this.dgExpandedPFT.Records[ e.Cell.Record.Index ].FieldLayout.Fields[ 7 ].Settings.AllowEdit = true; this.dgExpandedPFT.Records[ e.Cell.Record.Index ].FieldLayout.Fields[ 8 ].Settings.AllowEdit = true; this.dgExpandedPFT.Records[ e.Cell.Record.Index ].FieldLayout.Fields[ 9 ].Settings.AllowEdit = true; this.dgExpandedPFT.Records[ e.Cell.Record.Index ].FieldLayout.Fields[ 10 ].Settings.AllowEdit = false; break; } default: { this.dgExpandedPFT.Records[ e.Cell.Record.Index ].FieldLayout.Fields[ 3 ].Settings.AllowEdit = true; this.dgExpandedPFT.Records[ e.Cell.Record.Index ].FieldLayout.Fields[ 4 ].Settings.AllowEdit = true; this.dgExpandedPFT.Records[ e.Cell.Record.Index ].FieldLayout.Fields[ 5 ].Settings.AllowEdit = true; this.dgExpandedPFT.Records[ e.Cell.Record.Index ].FieldLayout.Fields[ 6 ].Settings.AllowEdit = true; this.dgExpandedPFT.Records[ e.Cell.Record.Index ].FieldLayout.Fields[ 7 ].Settings.AllowEdit = true; this.dgExpandedPFT.Records[ e.Cell.Record.Index ].FieldLayout.Fields[ 8 ].Settings.AllowEdit = true; this.dgExpandedPFT.Records[ e.Cell.Record.Index ].FieldLayout.Fields[ 9 ].Settings.AllowEdit = true; this.dgExpandedPFT.Records[ e.Cell.Record.Index ].FieldLayout.Fields[ 10 ].Settings.AllowEdit = true; break; } } }
What do you think?
Thanks,
I have been looking into your code and I can say that this way you make all the Cells in a particular Field Editable or not, by setting the Field’s AllowEdit Property, but if it works for your scenario, I don’t see any reason why not to use it instead of the approach I gave you.
Thanks for that. On another issue my client says that he wants tabbing behavior to work so that the users can only tab to a cell that is editable. I've trapped the PreviewKeyDown message and have managed to know when the tab key has been pressed and I can find the row and column positions of the cell.
I have two questions, first how do I set an active cell? If I know that the next editible cell is on the same row, but two cells over, how do I position them there?
The second question is about how the grid behaves when tabbing. I have the tab order on the grid set to 3, and yet when I start tabbing through controls, it seems to skip over the grid and won't tab into it. How do I make that happen?
Thanks again for all of your help.
Mike.
I can suggest you see this forum thread:
http://forums.infragistics.com/forums/t/7243.aspx
where there is a sample (“xamDataGridTabSkip.zip”) with the functionality you want. Please let me know if this helps you or you need further assistance on this matter.
Thanks, Stefan for all of your help.
I have been looking into your code, and I can say that it is a very good approach for achieving the thing you want.
I did manage to get the result I was looking for by trapping the GotFocus message on the grid, and then filtering on the OriginalSource to prevent a stack overflow. Here is my code. Do you think it's ok to take this approach, or is there a better one you can think of?
private void dgExpandedPFT_GotFocus( object sender, RoutedEventArgs e ) { if( e.OriginalSource == this.dgExpandedPFT ) { Record[] records = this.dgExpandedPFT.GetRecordsInView( false ); this.dgExpandedPFT.ActiveRecord = records[ 0 ]; this.dgExpandedPFT.ActiveRecord.DataPresenter.ExecuteCommand( DataPresenterCommands.CellFirstInRecord ); this.dgExpandedPFT.ActiveRecord.DataPresenter.ExecuteCommand( DataPresenterCommands.StartEditMode ); } }
Ah, Stefan - I am fast becomming a fan of yours. Thanks so much, the link gave me what I needed. This, coupled with how I am setting cell editing permissions work together very well and now if a user clicks on cell that is not editable, it goes to the first cell in the row that is editable and I can only tab into cells that are editable. This works really nicely.
I have one last question. My grid is set to tab order 3 and when I tab from the control before it, to the grid, I can see that the entire grid has the focus. What I want is for the tab to go not to the entire grid, but into the first editable cell. How do I do that?