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

Display text in combobox cell in place of value when value is not in datasource

Hello,

I would like to display a text value a cell of the GridGroupingControl based on the value of the field in the dataset. My cell is a combobox which will be filled with text based on a set of values. However I would also like to handle the case where the value is not in the list and edit the displayed text according. To give the real life example here are the values:

7 = 1 week
90 = 1 month
365 = 1 year
730 = 2 years

I have 4 possible values in the combobox. However if there is a value not in the list, say 10, in the database, I would like to display 10 days in this case. If the value is not in the list I will always want to display the value followed by the text " days".

Also to note is that the combobox cell will be editable whereby the user can change the duration to another value. After which I will want to save the dataset and write the value to the database,not the text. So if the user selected 1 week, I will want to write 7 to the database.

Thanks for your help
Lisa



5 Replies

AR Amal Raj U Syncfusion Team September 26, 2016 12:49 PM UTC

Hi Lisa, 
 
Thanks for using Syncfusion products. 
 
We have analyzed your scenario.  
 
In order to have value member display member options for combo box cell types, then  the ValueMember and DisplayMember of the columns must be specified. Please make use of the below code, 
 
Code Example 
private DataTable GetTheComboTable() 
{ 
    DataTable dt = new DataTable("ComboValues"); 
    dt.Columns.Add(new DataColumn("Value", typeof(int))); 
    dt.Columns.Add(new DataColumn("Text",typeof(string))); 
    dt.Rows.Add(new object[] { 7, "1 Week" }); 
    dt.Rows.Add(new object[] { 30, "1 Month" }); 
    dt.Rows.Add(new object[] { 365, "1 Year" }); 
    dt.Rows.Add(new object[] { 730, "2 Years" }); 
    return dt; 
} 
 
comboTable = this.GetTheComboTable(); 
gridGroupingControl1.TopLevelGroupOptions.ShowAddNewRecordBeforeDetails = false; 
gridGroupingControl1.TableDescriptor.Columns["ComboValues"].Appearance.AnyRecordFieldCell.CellType = GridCellTypeName.ComboBox; 
gridGroupingControl1.TableDescriptor.Columns["ComboValues"].Appearance.AnyRecordFieldCell.DataSource = this.comboTable; 
gridGroupingControl1.TableDescriptor.Columns["ComboValues"].Appearance.AnyRecordFieldCell.DisplayMember = "Text"; 
gridGroupingControl1.TableDescriptor.Columns["ComboValues"].Appearance.AnyRecordFieldCell.ValueMember = "Value"; 
 
The reported scenario of displaying the display text and adding the cell values dynamically to combo box items can be achieved using the TableControlCurrentCellEditingComplete event. If the newly typed value is not in the existing combo box items, then the new item can be added to items source through ListControlPart.DataSource using the current cell renderer of the Combobox. Please make use of the below code, 
 
Code Example 
//Event subscription. 
this.gridGroupingControl1.TableControlCurrentCellEditingComplete += new GridTableControlEventHandler(gridGroupingControl1_TableControlCurrentCellEditingComplete); 
 
void gridGroupingControl1_TableControlCurrentCellEditingComplete(object sender, GridTableControlEventArgs e) 
{ 
    GridCurrentCell currentCell= e.TableControl.CurrentCell; 
    GridTableCellStyleInfo style = e.TableControl.GetTableViewStyleInfo(currentCell.RowIndex, currentCell.ColIndex); 
 
    if (style.TableCellIdentity != null && style.TableCellIdentity.Column != null && style.TableCellIdentity.Column.Name == "ComboValues") 
    { 
        GridDropDownGridListControlCellRenderer renderer = e.TableControl.CurrentCell.Renderer as GridDropDownGridListControlCellRenderer; 
 
        if (!renderer.ListControlPart.Items.Contains(style.CellValue)) 
        { 
            int cellValue; string displayText; 
            if (int.TryParse(style.CellValue.ToString(), out cellValue)) 
            { 
                if (cellValue % 7 == 0) 
                    displayText = cellValue / 7 + " Week"; 
                else if (cellValue % 30 == 0) 
                    displayText = cellValue / 30 + " Month"; 
                else if (cellValue % 365 == 0) 
                    displayText = cellValue / 365 + " Year"; 
                else 
                    displayText = cellValue + " Days"; 
 
                DataRow newRow = comboTable.NewRow(); 
                newRow["Value"] = cellValue; 
                newRow["Text"] = displayText; 
 
                comboTable.Rows.Add(newRow); 
                renderer.ListControlPart.DataSource = comboTable; 
            } 
        } 
    } 
} 
 
Sample Link 
 
Regards, 
Amal Raj U. 



LI Lisa September 27, 2016 12:18 PM UTC

Hi Amal, thank you for your repsonse, however your solution does not cater for the need to handle values in the database not in the combobox datasource.

I believe I do need to interpret an event, but just not the one you mentioned. The problem is that the dataset has values for the field duration which are outside the list of values 7, 90, 365, 730. If for example the value 10 is in the duration field, then I want to grid to show 10 days (since the value member will not match a value in the combotable). To be clear, the while happen at the reading of the data, not on editing the data in the grid. If the ms grid I could use the CellFormatting event for this. I cannot see the corresponding event in the GridGroupingControl?


AR Amal Raj U Syncfusion Team September 28, 2016 01:22 PM UTC

Hi Lisa, 

Thanks for the update. 

The reported scenario of drawing the display text in data source loading can be achieved using the TableControlDrawCellDisplayText event. Please make use of the below code, 
 
Code Example 
//Event subscription. 
this.gridGroupingControl1.TableControlDrawCellDisplayText += new GridTableControlDrawCellDisplayTextEventHandler(gridGroupingControl1_TableControlDrawCellDisplayText); 
 
void gridGroupingControl1_TableControlDrawCellDisplayText(object sender, GridTableControlDrawCellDisplayTextEventArgs e) 
    GridTableCellStyleInfo style = e.TableControl.GetTableViewStyleInfo(e.Inner.RowIndex, e.Inner.ColIndex); 
    if (style.TableCellIdentity != null && style.TableCellIdentity.DisplayElement.Kind == DisplayElementKind.Record &&  
        style.TableCellIdentity.Column != null && style.TableCellIdentity.Column.Name == "ComboValues"
    { 
        DataTable dt = style.DataSource as DataTable
        List<String> items = new List<string>(); 
        foreach (DataRow row in dt.Rows) 
        { 
            items.Add(row["Value"].ToString()); 
        } 
        if (!items.Contains(style.CellValue.ToString())) 
        { 
            int cellValue; string displayText; 
            if (int.TryParse(style.CellValue.ToString(), out cellValue)) 
            { 
                if (cellValue % 7 == 0) 
                    displayText = cellValue / 7 + " Week"
                else if (cellValue % 30 == 0) 
                    displayText = cellValue / 30 + " Month"
                else if (cellValue % 365 == 0) 
                    displayText = cellValue / 365 + " Year"
                else 
                    displayText = cellValue + " Days"
 
                e.Inner.DisplayText = displayText; 
            } 
        } 
    } 
 
Sample Link 
 
Regards, 
Amal Raj U. 



LI Lisa September 28, 2016 02:44 PM UTC

Hi Amal,

Thank you very much, that is exactly what I was looking for.

Regards,
Lisa


AR Amal Raj U Syncfusion Team September 29, 2016 04:33 AM UTC

Hi Lisa, 

Thanks for the update. 

We are glad to know that the provided solution in our last update has resolved your query. Please let us know, if you have any other concerns. 

Regards, 
Amal Raj U. 


Loader.
Up arrow icon