Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
25
Add new record template
posted

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?

 

Parents
  • 340
    Offline posted

    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!

Reply Children