We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How to display combo box DisplayMember when grouping

I have gridgrouping control with combo boxes. When application is runing and I place this column on the grouping it shows the ValueMember and I would like it to display Display Memeber of the combo box. Basicaly it displays ColumnID of the data instead of the let say name.

//bind datasouse to combobox
            gGCProposals.TableDescriptor.Columns["ProposalGroup"].Appearance.AnyRecordFieldCell.DataSource = ProposalGroupDataTable;
            gGCProposals.TableDescriptor.Columns["ProposalGroup"].Appearance.AnyRecordFieldCell.DisplayMember = "ProposalGroupName";
            gGCProposals.TableDescriptor.Columns["ProposalGroup"].Appearance.AnyRecordFieldCell.ValueMember = "ProposalGroupID";


11 Replies

AA Arulraj A Syncfusion Team December 17, 2018 07:23 AM UTC

Hi Mariusz, 

Thanks for using Syncfusion product. 

To display the DisplayMember value in GroupCaptionCell, you could use the CellValue property in QueryCellStyleInfo event. Please refer the following code example and the sample. 

Code example 
this.gridGroupingControl1.QueryCellStyleInfo += GridGroupingControl1_QueryCellStyleInfo; 
 
 
private void GridGroupingControl1_QueryCellStyleInfo(object sender, GridTableCellStyleInfoEventArgs e) 
{ 
    if (e.TableCellIdentity == null) 
        return; 
 
    if (e.TableCellIdentity.TableCellType == GridTableCellType.GroupCaptionCell && e.TableCellIdentity.GroupedColumn != null) 
    { 
        GridCaptionRow captionRow = e.TableCellIdentity.DisplayElement as GridCaptionRow; 
 
        var style = this.gridGroupingControl1.TableDescriptor.Columns[e.TableCellIdentity.GroupedColumn.Name].Appearance.AnyRecordFieldCell; 
       if (style != null) 
        { 
            var data = style.DataSource as DataTable; 
            if(data != null) 
            { 
                var row = data.Select("[" + style.ValueMember + "] = '" + e.TableCellIdentity.DisplayElement.ParentGroup.Category + "'"); 
                if (row != null) 
                { 
                    string displayMember = row[0][style.DisplayMember].ToString(); 
                    e.Style.CellValue = String.Format("{0}:{1}-{2} items", e.TableCellIdentity.GroupedColumn.Name, displayMember, e.TableCellIdentity.DisplayElement.ParentGroup.GetChildCount()); 
                } 
            } 
        } 
    } 
} 
 

Let us know whether this helps also if you need any further assistance on this. 

Arulraj A 



MJ Mariusz Juszkiewicz December 18, 2018 03:40 AM UTC

I get an error:


AA Arulraj A Syncfusion Team December 18, 2018 06:46 AM UTC

Hi Mariusz, 

Thanks for your update. 

Can you please confirm whether that you are facing this issue in our provided sample or your application. The reported issue occurs when you try to access outside of the row array. You could avoid this exception by condition checking. Please refer the following code example. 

C# 
var row = data.Select("[" + style.ValueMember + "] = '" + e.TableCellIdentity.DisplayElement.ParentGroup.Category + "'"); 
if (row != null && row.Length > 0) 
{ 
    string displayMember = row[0][style.DisplayMember].ToString(); 
    e.Style.CellValue = String.Format("{0}:{1}-{2} items", e.TableCellIdentity.GroupedColumn.Name, displayMember, e.TableCellIdentity.DisplayElement.ParentGroup.GetChildCount()); 
} 
 
 
Let us know whether this helps also if you need any further assistance on this. 
 
Arulraj A 



MJ Mariusz Juszkiewicz December 19, 2018 05:02 AM UTC

I am facing the issue in my application. The example works perfectly. I can't this to work with my app. I think this ability to show the display member should be built in into the control. I work with sql database have broke the referential integrity to make this work i have used [name] as the key instead of id field. Thanks.MJ


AA Arulraj A Syncfusion Team December 19, 2018 12:28 PM UTC

Hi Mariusz, 
 
Thanks for your update. 
 
In our Provided sample, ValueMember is Id or Name whatever that is doesn’t matter ValueMember should be presented in your DataTable column then it will select the specified category value. So, if it is possible please provide your simple application to check your reported scenario. It will be helpful us to provide the solution at the earliest. 
 
Arulraj A 



MJ Mariusz Juszkiewicz December 22, 2018 03:25 AM UTC

Arulraj,

I got this to work with your code. Great job thanks!


AA Arulraj A Syncfusion Team December 24, 2018 12:44 AM UTC

Hi Mariusz,

Thanks for the update.

We are glad to know that the reported problem has been resolved at your end. Please let us know if you have any further queries on this. We are happy to help you.

Regards,
Arulraj A


MJ Mariusz Juszkiewicz January 8, 2020 05:22 AM UTC

This has been working for we well until I have introduced two new comboboxes which datasource is loaded during the same event QueryCellStyleInfo. Since then when I select column with this combobox the Group header formating changes to default. below is the code: if you need my project code let me know.
private void gridGroupingControlAll_QueryCellStyleInfo(object sender, GridTableCellStyleInfoEventArgs e)
        {
            if (e.TableCellIdentity == null)
                return;

            //select datasource for the Major code comboboxes
            if (e.TableCellIdentity.Column != null && e.TableCellIdentity.Column.Name == "EstDataMajor"
                && e.TableCellIdentity.DisplayElement.Kind == Syncfusion.Grouping.DisplayElementKind.Record)
            {
                // Gets the value selected from the combobox.
                string divisionID = e.TableCellIdentity.DisplayElement.GetRecord().GetValue("EstDataDivision").ToString();
                string Fillquerry = "SELECT MajorID, CONCAT(RTRIM(MajorID), ' - ',RTRIM(MajorDescription)) AS Description FROM dbo.CodeMajor WHERE DivisionID='" + divisionID + "'";
                DataTable MajorDT = Dbo.ReturnDataTable(Fillquerry, Properties.Settings.Default.DBConnString);
                e.Style.DataSource = MajorDT;
                e.Style.DisplayMember = "Description";
                e.Style.ValueMember = "MajorID";
            }

            //select datasource for the Minor code comboboxes
            if (e.TableCellIdentity.Column != null && e.TableCellIdentity.Column.Name == "EstDataMinor"
                && e.TableCellIdentity.DisplayElement.Kind == Syncfusion.Grouping.DisplayElementKind.Record)
            {
                // Gets the value selected from the combobox.
                string majorID = e.TableCellIdentity.DisplayElement.GetRecord().GetValue("EstDataMajor").ToString();
                string Fillquerry = "SELECT MinorID, CONCAT(RTRIM(MinorID), ' - ',RTRIM(MinorDecription)) AS Description FROM dbo.CodeMinor WHERE MajorID='" + majorID + "'";
                DataTable MinorDT = Dbo.ReturnDataTable(Fillquerry, Properties.Settings.Default.DBConnString);
                e.Style.DataSource = MinorDT;
                e.Style.DisplayMember = "Description";
                e.Style.ValueMember = "MinorID";
            }
            //Display grouping name
            if (e.TableCellIdentity.TableCellType == GridTableCellType.GroupCaptionCell && e.TableCellIdentity.GroupedColumn != null)
            {

                var style = this.gridGroupingControlAll.TableDescriptor.Columns[e.TableCellIdentity.GroupedColumn.Name].Appearance.AnyRecordFieldCell;
                if (style != null)
                {
                    var data = style.DataSource as DataTable;
                    if (data != null)
                    {
                        var row = data.Select("[" + style.ValueMember + "] = '" + e.TableCellIdentity.DisplayElement.ParentGroup.Category + "'");
                        if (row != null && row.Length > 0)
                        {
                            string displayMember = row[0][style.DisplayMember].ToString();
                            e.Style.CellValue = String.Format("{0}-{1} items", displayMember, e.TableCellIdentity.DisplayElement.ParentGroup.GetChildCount());
                        }
                    }
                }
            }
        }


AR Arulpriya Ramalingam Syncfusion Team January 9, 2020 10:36 AM UTC

Hi Mariusz, 
 
Thank you for the update. 
 
From the provided code, we could notice that the datasource for ComboBox cells are assigned and it is retrieved from the AnyRecordFieldCell. So, the datasource will remain null and the group caption will not be updated. In order to overcome this scenario, we would suggest you to set the datasource for AnyRecordFieldCell of the column. Please make use of below code and modified sample. 
 
Example code 
  
private void GridGroupingControl1_QueryCellStyleInfo(object sender, GridTableCellStyleInfoEventArgs e) 
 { 
     if (e.TableCellIdentity == null) 
         return; 
 
     //select datasource for the Major code comboboxes 
     if (e.TableCellIdentity.Column != null && e.TableCellIdentity.Column.Name == "Ship City" 
         && e.TableCellIdentity.DisplayElement.Kind == Syncfusion.Grouping.DisplayElementKind.Record) 
     { 
         // Gets the value selected from the combobox.  
         //string divisionID = e.TableCellIdentity.DisplayElement.GetRecord().GetValue("EstDataDivision").ToString(); 
         //string Fillquerry = "SELECT MajorID, CONCAT(RTRIM(MajorID), ' - ',RTRIM(MajorDescription)) AS Description FROM dbo.CodeMajor WHERE DivisionID='" + divisionID + "'"; 
         //DataTable MajorDT = Dbo.ReturnDataTable(Fillquerry, Properties.Settings.Default.DBConnString); 
         e.TableCellIdentity.Column.Appearance.AnyRecordFieldCell.CellType = GridCellTypeName.ComboBox; 
         e.TableCellIdentity.Column.Appearance.AnyRecordFieldCell.DataSource = MajorDT; 
         e.TableCellIdentity.Column.Appearance.AnyRecordFieldCell.DisplayMember = "Description"; 
         e.TableCellIdentity.Column.Appearance.AnyRecordFieldCell.ValueMember = "MajorID"; 
 
     } 
 
     //Do the Same for all combobox columns. 
     // code to update/change the DisplayMember as grouping name 
} 
 
 
Please get back to us, if you have any other queries. 
 
Regards, 
Arulpriya 



MJ Mariusz Juszkiewicz January 15, 2020 04:49 AM UTC

Thanks. I have tried the code provided it works well when the column is displayed all the time. When the column is hidden or not in view the gouping shows default formatting.


AR Arulpriya Ramalingam Syncfusion Team January 17, 2020 10:55 AM UTC

Hi Mariusz, 
 
Thank you for the update. 
 
We could understand the scenario and as per the current behavior of GridGroupingControl QueryCellInfo event, it will trigger only for the cells that in the visible range. So, the datasource for the ComboBox column will be null and the display member will not be retrieved when the column is hidden or not in the visible range. In order to avoid the use case, we would suggest that, to set the default datasource for the ComboBox columns while loading the form and update the data source for each cell based on the cell value in QueryCellInfo event when the column moves to visible range. 
 
 
Please get back to us, if you have any other queries. 
 
Regards, 
Arulpriya 


Loader.
Live Chat Icon For mobile
Up arrow icon