Is there a way to template add new record row?
I have an entity with a primary key column and a type column which are set to read only and a few other columns which are editable.
class Mammal
{
string ID{get;set;}
Type{get;set;} // only Two options : Animal, Human
double Weight{get;set;}
int age{get;set;}
string Description{get;set;}
}
I need the ID and Type as read only even in the add new row. For some screens, I need enter only humans. For those screens, I'm trying to prefill the add new row with ID as "NEW" and Type as "Human", both read only and not allowing the user to type in those columns. The only way so far I found was to initialize the fields in the default constructor, but even then, the fields are editable.
1. How can I make a column readonly in add new row?
2. How can I prefill values in add new row for the above read only columns?
I'm hoping the mkadiri was able to resolve his issue in the time since his post, but I was having some difficulty with this earlier today and was unable to find any information on the topic online. After some trial and error, I was able to find a solution, so I figured I would post it here in case anyone else stumbled across this post as I did.
If anyone knows a better way of doing this, please let me know. Also, I know I made some assumptions on how the grids work behind the scenes, so feel free to correct me if you have more information.
While you can handle the InitializeRecord event, check for an empty ID, and supply the default values, I ran into a few issues with bindings when I tried to set up some more complex behavior on the grid (a button to mark rows for batch deletion). Then I discovered the InitializeTemplateAddRecord event, which seemed to be exactly what I wanted, but it's behavior confused me and the API documentation was no help.
It looks like this event allows you to process the add record before the associated DataItem has been created. By calling SetCellValue, you can provide a default value that will be used upon creation. Just be careful, because if a cell editor sets up a binding on this value, and it's changed before the data item is created, the data item will inherit the new value. Basically, a new add record gets created once upon load, and then after each commit. The DataItem doesn't get created until a cell in the add record enters edit mode, and inherits the current cell values of the TemplateAddRecord at the time.
That's a lot of explanation for what is really fairly simple code:
private void uxGrid_InitializeTemplateAddRecord(object sender, InitializeTemplateAddRecordEventArgs e){ e.TemplateAddRecord.SetCellValue(e.TemplateAddRecord.FieldLayout.Fields[0], "Value"); }
You'll probably want to mine the desired field out of the array more elegantly, of course, but I just wanted to provide a generic example. Hope this helps someone!
Hello,
Just to add some things to this thread:
There are couple more events to consider - RecordAdded/RecordAdding. The Current Add Record is exposed by the CurrentAddRecord property of the RecordManager object of the XamDataGrid. If you want to set values somewhere outside the InitializeTemplateAddRecord event, here is how to do that:
xamDataGrid1.RecordManager.CurrentAddRecord.Cells[0].Value = 5;
Regarding the second question of the initial post - how to make a cell readonly, you can simply cancel entering the edit mode. You can handle EditModeStarting event and check if that record is the CurrentAddRecord and set e.Cancel = true;
Both the FieldLayout.Fields and TemplateAddRecord.Cell collection have string indexers for the name of the Fiels :
e.TemplateAddRecord.Cells["Name"].Value = "new Name";e.TemplateAddRecord.SetCellValue(e.TemplateAddRecord.FieldLayout.Fields["Name"],"new Name");
Moreover, the Cells collection has third indexer of type Field.
private void uxGrid_InitializeTemplateAddRecord(object sender, InitializeTemplateAddRecordEventArgs e){ e.TemplateAddRecord.SetCellValue(e.TemplateAddRecord.FieldLayout.Fields[0], "Value");}
Alex, I'm wondering if you can help me find a more elegant way of determining the field, like using the field name and not the index. I am using the TemplateAddRecord event to prepopulate new records in my grid and when I change the field layout I keep getting bit by this. Thanks.
Thanks Alex, I appreciate the clarifications. While the InitializeTemplateAddRecord seems to do what I want, I'll give the RecordAdding implementation a try to get to know the grids a little better. I'm sure I'll be needing the second tip before long, as well. :)