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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to display combo box DisplayMember when grouping

Thread ID:

Created:

Updated:

Platform:

Replies:

141526 Dec 15,2018 08:21 PM UTC Jan 17,2020 10:55 AM UTC WinForms 11
loading
Tags: GridGroupingControl
Mariusz Juszkiewicz
Asked On December 15, 2018 08:21 PM UTC

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";


Arulraj A [Syncfusion]
Replied On 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 


Mariusz Juszkiewicz
Replied On December 18, 2018 03:40 AM UTC

I get an error:

Arulraj A [Syncfusion]
Replied On 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 


Mariusz Juszkiewicz
Replied On 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

Arulraj A [Syncfusion]
Replied On 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 


Mariusz Juszkiewicz
Replied On December 22, 2018 03:25 AM UTC

Arulraj,

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

Arulraj A [Syncfusion]
Replied On 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

Mariusz Juszkiewicz
Replied On 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());
                        }
                    }
                }
            }
        }

Arulpriya Ramalingam [Syncfusion]
Replied On 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 


Mariusz Juszkiewicz
Replied On 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.

Arulpriya Ramalingam [Syncfusion]
Replied On 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 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon