GrigComboBoxColumn foreign key

Hi,

I've two tables with relation:

userTable table13UtentiBindingSource

Id UserName

1  Max

2  Flo

productTable table02ProdottiBindingSource

Id Product IdUser

1  ProdA    2

2  ProdB    1

and want to show an additional column in sfDataGridProducts with

sfDataGridProducts.Columns.Add(new GridComboBoxColumn() { MappingName = "IdUser", HeaderText = "User", DisplayMember = "UserName", ValueMember = "IdUser", DropDownStyle = DropDownStyle.DropDownList, DataSource = table13UtentiBindingSource });

but this column show the userTable Id and not the  UserName as expected.

Thanks.


9 Replies 1 reply marked as answer

DM Dhanasekar Mohanraj Syncfusion Team August 30, 2022 05:14 PM UTC

Hi Massimo,

We have checked the reported issue on our end. In that, you are using the GridComboBoxColumns valumember as IdUser which is not present in the datasouce bound to the GridCOmboBoxColumn. However, you can overcome the reported issue using the below code snippet,

this.sfDataGrid1.Columns.Add(new GridComboBoxColumn() { MappingName = "IdUser", HeaderText = "User", DisplayMember = "UserName", ValueMember = "ID", DropDownStyle = DropDownStyle.DropDownList, DataSource = this.GetTable() });


We have prepared the sample based on your scenario,

Sample link: https://www.syncfusion.com/downloads/support/forum/177122/ze/SfDatagridDemo445522598 

Please let us know if you have any concerns about this.

Regards,

Dhanasekar M.



If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.



MA Massimo September 2, 2022 06:45 AM UTC

Hi  Dhanasekar,

as you suggest I used  ValueMember = "ID" but the value showed in the column is the Id of the userTable.

Just when I doubleclick on the cell for editing id shows me the User



DM Dhanasekar Mohanraj Syncfusion Team September 5, 2022 03:06 PM UTC

Hi Massimo,

We have checked the reported issue on our end. The valve member is used to retrieve the selected Item only. You will achieve your requirement to display the ID value while the column is in the edit is achieved using the custom renderer for GridComboBocColumn shown below,

this.sfDataGrid1.CellRenderers.Remove("ComboBox");

this.sfDataGrid1.CellRenderers.Add("ComboBox", new GridComboBoxCellRendererExt(sfDataGrid1));


public
class GridComboBoxCellRendererExt : GridComboBoxCellRenderer

{

    SfDataGrid dataGrid;

    SfComboBox SfCombo;

    public GridComboBoxCellRendererExt(SfDataGrid sfDataGrid)

    {

        dataGrid = sfDataGrid;

    }

 

    protected override void OnInitializeEditElement(DataColumnBase column, RowColumnIndex rowColumnIndex, SfComboBox uiElement)

    {

        base.OnInitializeEditElement(column, rowColumnIndex, uiElement);

        uiElement.DisplayMember = "ID";

    }

}


We have prepared the sample based on your scenario. 
Please find the sample in the attachment and let us know if you have any concerns about this.


Regards,
Dhanasekar M.


If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.


Attachment: SfDatagridDemo_ba6788d8.zip


MA Massimo September 5, 2022 04:52 PM UTC

Hi  Dhanasekar,

in both your samples the GridComboBoxColumn column shows the correct value UserName,

in my sample with or without  GridComboBoxCellRendererExt the  GridComboBoxColumn  shows the Id and not the UserName.

I'm talking about the initial render and not when in edit mode (with double click).

My sample show correctly the UserName when I doubleclick on the cell. But my goal is to show the UserName in the initial render.

Best Regards,



DM Dhanasekar Mohanraj Syncfusion Team September 6, 2022 02:37 PM UTC

Hi Massimo,

We have checked the reported issue on our end. Can achieve your requirement to show the UserName in the initial itself can be achieved by setting the UserName as a DisplayMember Shown below

this.sfDataGrid1.Columns.Add(new GridComboBoxColumn() { MappingName = "IdUser", HeaderText = "User", DisplayMember = "UserName", ValueMember = "ID",  DataSource = this.GetTable() });


Notes:

  1. The Valuemenber and the Columns underlying property is should be the same if it has different datatypes this kind of issue occurs.

  2. In the attached sample ID column connect the two different DataTables, If you are trying to connect the Datatbles it should have the same property with the same datatypes.

Please find the sample in the attachment and let us know if you have any concerns about this.

Regards,

Dhanasekar M.

If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.


Attachment: SfDatagridDemo__c6d389f8.zip


MA Massimo September 7, 2022 06:48 PM UTC

Hi  Dhanasekar, 

I have some problems explaining myself, so here is attached a sample reporting the above problem in a SqLite DB.

There is also a file Db.txt explaining the two tables.

So, the goal is to show the UserName value in the column User of the sfDataGrid2.

Regards,


Attachment: WindowsFormsApp1_97801dfb.zip


VS Vijayarasan Sivanandham Syncfusion Team September 8, 2022 03:10 PM UTC

Hi Massimo,

We have checked the provided sample from our end. The GridComboBoxColumn was added after the DataSource was assigned in SfDataGrid. In this case, the columns will be automatically generated based on its column type from the underlying data source. After adding the GridComboBoxColumn initial loading the data displayed as IdUser value. Please refer to the below screenshot,



The reported problem occurs while defining the duplicate column for the same property (ex: IdUser).

You can resolve the reported problem by changing the column type using the AutoGeneratingColumn event in SfDataGrid. Please refer to the below code snippet,

this.sfDataGrid2.AutoGeneratingColumn += OnAutoGeneratingColumn;

 

private void OnAutoGeneratingColumn(object sender, Syncfusion.WinForms.DataGrid.Events.AutoGeneratingColumnArgs e)

{

            if (e.Column.MappingName == "IdUser")

            {

                //here Changing Column Type as GridComboBoxColumn

                e.Column =new GridComboBoxColumn() { MappingName = "IdUser", HeaderText = "User", DisplayMember = "UserName", ValueMember = "Id", DropDownStyle = DropDownStyle.DropDownList, DataSource = userDataTable };

            }

}


UG Link: https://help.syncfusion.com/windowsforms/datagrid/columns#changing-column-type

Please find the modified sample in the attachment and let us know if you have any concerns in this.


Regards,

Vijayarasan S


If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.


Attachment: ModifiedSample_bdc090f8.zip

Marked as answer

MA Massimo September 8, 2022 07:53 PM UTC

Hi  Vijayarasan,

thank you very much, you solve my problem.

Regards,



DM Dhanasekar Mohanraj Syncfusion Team September 9, 2022 02:03 PM UTC

Hi Massimo,


If you are satisfied with our response, please mark it as an answer. Otherwise, please let us know if you have any further queries on this. We are happy to help you.


Regards,

Dhanasekar M.


Loader.
Up arrow icon