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
close icon

Format DisplayMember in GGC and in ComboboxAdv

Hi, I have a combo in both GGC and cboAdv where I have to format a date, for say "dd/MM/yy" in the DisplayMember. The ValueMember is an integer, how can I format the DisplayMember? Thanks in advance! - Nicolas

3 Replies

PM Piruthiviraj Malaimelraj Syncfusion Team November 2, 2016 01:26 PM UTC

Hi Nicolas, 
 
Thanks for your interest in Syncfusion products. 
 
In order to apply the format for display member of the ComboBox , you need to customize the GridComboBoxCellModel and GidComboBoxCellRenderer. Please make use of the below code, 
 
Code example 
 
this.gridGroupingControl1.TableModel.CellModels.Add("CustomComboBox", new GridComboBoxCellModelAdv(this.gridGroupingControl1.TableModel)); 
 
this.gridGroupingControl1.TableDescriptor.Columns["Description"].Appearance.AnyRecordFieldCell.CellType = "CustomComboBox"; 
this.gridGroupingControl1.TableDescriptor.Columns["Description"].Appearance.AnyRecordFieldCell.DataSource = GetTable(); 
this.gridGroupingControl1.TableDescriptor.Columns["Description"].Appearance.AnyRecordFieldCell.DisplayMember = "Value"; 
this.gridGroupingControl1.TableDescriptor.Columns["Description"].Appearance.AnyRecordFieldCell.ValueMember = "ID"; 
this.gridGroupingControl1.TableDescriptor.Columns["Description"].Appearance.AnyRecordFieldCell.Format = "dd/MM/yy"; 
 
//Custom_ComboBoxCellModel: 
public class GridComboBoxCellModelAdv : GridComboBoxCellModel 
{ 
    public GridComboBoxCellModelAdv(GridModel grid) 
        : base(grid) 
    { 
        AllowFloating = false; 
        ButtonBarSize = new Size(SystemInformation.VerticalScrollBarWidth, 0); 
        SupportsChoiceList = true; 
    } 
 
    public override GridCellRendererBase CreateRenderer(GridControlBase control) 
    { 
        return new GridComboBoxCellRendererAdv(control, this); 
    } 
 
    public override string GetFormattedText(GridStyleInfo style, object value, int textInfo) 
    { 
        if (style.Format != string.Empty) 
        { 
            DateTime date = new DateTime(); 
            if (DateTime.TryParse(base.GetFormattedText(style, value, textInfo), out date)) 
            { 
                value = date.ToString(style.Format); 
            } 
            else if (style.CellValueType == typeof(string)) 
            { 
                value = string.Format("{0:" + style.Format + "}", base.GetFormattedText(style, value, textInfo)); 
            } 
        } 
 
        return base.GetFormattedText(style, value, textInfo); 
    } 
} 
//Custom_ComboBoxCellRenderer: 
public class GridComboBoxCellRendererAdv : GridComboBoxCellRenderer 
{ 
    public GridComboBoxCellRendererAdv(GridControlBase grid, GridCellModelBase cellModel) 
        : base(grid, cellModel) 
    { 
        DropDownImp.InitFocusEditPart = true; 
        DropDownButton = new GridCellComboBoxButton(this); 
    } 
 
    protected override void OnSetControlText(string text) 
    { 
        if (StyleInfo.Format != string.Empty) 
        { 
            DateTime date = new DateTime(); 
            if (DateTime.TryParse(text, out date)) 
            { 
                text = date.ToString(StyleInfo.Format); 
            } 
            else if (StyleInfo.CellValueType == typeof(string)) 
            { 
                text = string.Format("{0:" + StyleInfo.Format + "}", text); 
            } 
        } 
        base.OnSetControlText(text); 
    } 
} 
 
Sample link 

Regards, 
Piruthiviraj 



NI Nicolas November 3, 2016 04:20 PM UTC

Works great! Thank you! - Nicolas


PM Piruthiviraj Malaimelraj Syncfusion Team November 4, 2016 04:41 AM UTC

Hi Nicolas, 

Thanks for the update. 

We are glad to know that the given solution is resolved your scenario. Please let us know if you have any other queries. 

Regards, 
Piruthiviraj 


Loader.
Up arrow icon