Good morning I apologize my English, I am Italian and I recently started studying Applied C#. Specifically, I have to connect utilities to a grid through external keys to two other tables. I followed the examples but the linked fields on the grid remain empty. I attach the page. Could you help me solve the problem. Thanks George
Hi Gio,
Greetings from Syncfusion.
Before proceeding further we would like to clarify that by using Grid foreign key column we can perform data-mapping between two tables only if both ForeignKey table & Grid table have same mapping value. ( i.e. for example in your foreign table contains a field (ID_LOC/ID_TIPO_FORN) and if your parent has the same field (ID_LOC/ID_TIPO_FORN) then it can be used for comparison ) But in your shared code both the foreignkey table and Grid table doesn’t have a same field name. So the reported issue occurs.
If your field name is different but when it have same field value (both Grid table and foreign key table) you can use the ForeignkeyField property to perform proper mapping. Please refer the below attached code for your reference.
|
<GridForeignColumn Field=@nameof(Utenza.ID_LOC) ForeignKeyField="Id_Ubicazione" HeaderText="LOCALITA" Width="150" ForeignKeyValue="Ubicazione" ForeignDataSource="@localita"> </GridForeignColumn>
|
Also a primary key from one table cannot directly serve as a foreign key in another table. So please avoid declaring primarykey field as a foreignkey field. It will cause some issues when used with database.
Additional information :
Here in the below code snippet (ID_LOC/ID_TIPO_FORN) are used as a fields but it doesn’t have the same field in the foreign key table. Kindly try using ForeignKeyField property as suggested above. If you still face difficulties then kindly share us the database/duplicate database from your end. So that we will modify and provide you an updated sample as per your requirement.
|
<GridForeignColumn Field=@nameof(Utenza.ID_LOC) HeaderText="LOCALITA" Width="150" ForeignKeyValue="Ubicazione" ForeignDataSource="@localita"> </GridForeignColumn> <GridColumn Field=@nameof(Utenza.VIA) HeaderText="VIA" EditType="EditType.DefaultEdit" Width="150"></GridColumn> <GridColumn Field=@nameof(Utenza.ID_TIPO_FORN) HeaderText="TIPO FORNITURA" EditType="EditType.DropDownEdit" ValidationRules="@(new ValidationRules{ Required=false })" Width="150"></GridColumn> <GridForeignColumn Field=@nameof(Utenza.ID_TIPO_FORN) HeaderText="TIPO" Width="150" ForeignKeyValue="TIPO_FORN" ForeignDataSource="@tipo"> </GridForeignColumn> |
|
public class Utenza {
[Key] public int ID_UTENZA { get; set; }
[ForeignKey("ID_LOC")] public int ID_LOC { get; set; }
[ForeignKey(nameof(ID_TIPO_FORN))] public int ID_TIPO_FORN { get; set; }
}
|
|
public class TipoForn { [Key] public int Id { get; set; } public string? Tipo_forn { get; set; } }
|
|
public class Localita { [Key] public int Id_Ubicazione { get; set; } public string? Ubicazione { get; set; } } |
Regards,
Monisha
Ok thanks for the quick response. I'll edit the code as soon as possible and test if it works. Eventually I attach the db as you said. Again a greeting and a thank you.
Giorgio
I mapped the grid table and the external table with the same name as you said and everything works great. See you next tip :) thanks again
Hi Gio,
Thanks for the update. We are glad to hear that the reported issue has been resolved. Kindly get back to us if you have further queries. As always we will be happy to assist you.