Updating Entity Identity Field on Infragistics Grid

Posts   
 
    
tedh
User
Posts: 34
Joined: 14-Dec-2006
# Posted on: 14-Dec-2006 05:12:39   

I am a newbie running LLBLGenPro 2.0 demo with .Net 2.0.

I am displaying a list of currencies in an Infragistics version 6.3 UltraGrid. The columns in the grid are Code (string), Description (string) and Id (integer). These fields mirror the fields in the Currency Table in my SQL 2005 database. The Id field is an identity field in the database.

An example grid row is:

Code = CA, Description = Canadian $, ID = 1

I am using the LLBLGenPRO single class selfservicing model.

What I am trying to do is to add a new currency row to the grid and then persist it to the database. When the currency is persisted an Id is auto-generated by SQL 2005 and the grid is updated to show the assigned Id field.

I add new empty currency rows to the grid as follows:

private void ultraGrid1_AfterRowInsert(object sender, RowEventArgs e) { // Default values for new grid rows e.Row.Cells["Code"].Value = string.Empty; e.Row.Cells["Description"].Value = string.Empty; }

After the user enters a Code and Description a record is persisted to the database as follows:

            EntityClasses.CurrencyEntity newCurrency;
            newCurrency = new EntityClasses.CurrencyEntity(AddNewRecord(e.Row));
            // Update grid to display assigned database Id value
            e.Row.Cells["ID"].Value = newCurrency.Id;

The AddNewRecord method takes the grid row as a parameter and returns an integer which is the ID field for the new currency record. The AddNewRecord method performs correctly and the record is persisted to the database.

The assignment "e.Row.Cells["ID"].Value = newCurrency.Id;" fails with the error "The field 'Id' is read-only and can't be changed."

The error source is: "SD.LLBLGen.Pro.ORMSupportClasses.NET20" The runtime build is: "12052006" The runtime version is: "2.0.0.0"

Can you please tell me what I am doing wrong? I am just trying to update the Id cell in the grid.

Thanks tedh

Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 14-Dec-2006 06:05:41   

The field is generated as read-only in the entity class, since it's an identity field. However you can workaround this by grabbing the entity from the binded collection and do the following:

myCurrency.Fields[(int)CurrencyFieldindex.Id].ForcedCurrentValueWrite(newValue);

Although I believe you can save yourself a lot by refetching the currencies after adding a new currency. Or by using a 2-way Databinding techniques with the help of a binding source. Please refer to the LLBLGen Pro docs: "Usig the generated code -> SelfServicing -> Databinding with Windows Forms and ASP.NET 1.x"