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

Custom cell type dynamically based on Cell text

Hi Team,

I need to change the nested table specific cell type as Control based on Cell value. For example, need to set the child table cell type as control if parent record of corresponding child table row value matches some condition. 

The below code set all rows in the specified column as Checkbox. But I need to set the cell type like above.
this.gridGroupingControl1.TableDescriptor.Columns[2].Appearance.AnyRecordFieldCell.CellType = GridCellTypeName.CheckBox

I tried in QueryCellStyleInfo, based on column and row index I have changed the cell style as control. But instead of row and column index, I need to set the cell type based on Parent record value. 
Also, Is there any other way to achieve this? Because QueryCellStyleInfo called every time on cell action. My version is 14.2 and targetting .Net 3.5

Thanks,
Kevin

3 Replies

PM Piruthiviraj Malaimelraj Syncfusion Team March 21, 2017 10:29 AM UTC

Hi Kevin, 
 
Thanks for the update. 
 
We could able to understand your scenario. In order to achieve your scenario, QueryCelllStyleInfo event can be used. In the attached sample, we have set the “CheckBox” celltype for a column of child table , if parent record’s CategoryName(column name) value is “Beverages” .Please make use of the below code, 
 
Code example: 
this.gridGroupingControl1.QueryCellStyleInfo += new GridTableCellStyleInfoEventHandler(gridGroupingControl1_QueryCellStyleInfo); 
 
void gridGroupingControl1_QueryCellStyleInfo(object sender, GridTableCellStyleInfoEventArgs e) 
{ 
    if (e.Style.TableCellIdentity.DisplayElement.Kind == DisplayElementKind.Record) 
    { 
        Record record = e.Style.TableCellIdentity.DisplayElement.GetRecord(); 
         
        //To check the child table records. 
        if (record != null && record.ParentTable.FilteredChildTable != null 
            && record.Kind == DisplayElementKind.Record) 
        { 
            //Gets the parent record. 
            Record parentRecord = record.ParentChildTable.ParentNestedTable.ParentRecord; 
 
            //Set the celltype for child table column based on parent record. 
            if (parentRecord.GetValue("CategoryName").ToString() == "Beverages") 
            { 
                if (e.TableCellIdentity.Column != null && e.TableCellIdentity.Column.Name == "SupplierID") 
                { 
                    e.Style.CellType = "CheckBox"; 
                } 
            } 
        } 
    } 
} 
 
 
 
Sample link: 
 
Regards, 
Piruthiviraj


KE Kevin March 21, 2017 06:13 PM UTC

Hi Piruthiviraj,

Thanks for the update. I am having few more queries. In one page, I found the below code for getting parent record. 
Record parentRecord = e.TableCellIdentity.Table.TableModel.FilteredChildTable.ParentDisplayElement.ParentRecord;
In your code, you have mentioned some other way for getting parent record.
Record record = e.Style.TableCellIdentity.DisplayElement.GetRecord(); 
Record parentRecord = record.ParentChildTable.ParentNestedTable.ParentRecord; 

what is the exact difference between below two codes?
Record parentRecord = e.TableCellIdentity.Table.TableModel.FilteredChildTable.ParentDisplayElement.ParentRecord;
Record parentRecord = e.TableCellIdentity.DisplayElement.ParentChildTable.ParentNestedTable.ParentRecord;

Also as I like mentioned in my previous update, some of nested table cells having control and text. I have modified the sample that you shared and added exporting to excel options. But facing below issues.
1. Assigned text in cell which is having Checkbox control, but the text is not displayed in UI. If I exported, text is present in Excel.
2. Initially, On form load all my records is in collapse state. If I am exporting data to excel without doing expand, nested table cell values that are changed in QueryCellStyleInfo not exported properly. I dont have the need for expand the records before exporting. I have attaced the screenshots for this issues and modified sample project. 

Can you please share the solution for this?
- Kevin

Attachment: SampleModified_16d4ea98.zip


PM Piruthiviraj Malaimelraj Syncfusion Team March 22, 2017 09:33 AM UTC

Hi Kevin, 
 
Thanks for the update. 
 
Query 
Response 
what is the exact difference between below two codes? 
Record parentRecord = e.TableCellIdentity.Table.TableModel.FilteredChildTable.ParentDisplayElement.ParentRecord; 
Record parentRecord = e.TableCellIdentity.DisplayElement.ParentChildTable.ParentNestedTable.ParentRecord; 
 
These two code will returns the same record. These are the ways to get the corresponding parent record from child table record. So you can use any one of those methods to get the parent record. 
 
 
 
 
 
 
 
 
 
 
Assigned text in cell which is having Checkbox control, but the text is not displayed in UI 
In order to set the text for checkbox , use the Style.Description property instead of Style.Text. 
 
Code example: 
e.Style.CellType = GridCellTypeName.CheckBox; 
e.Style.Description = "CustomText"; 
 
 
 
 
 
 
Initially, On form load all my records is in collapse state. If I am exporting data to excel without doing expand, nested table cell values that are changed in QueryCellStyleInfo not exported properly. 
We could able to understand your scenario. By default, the child table records can be accessed in QueryCellStyleInfo event when that child table is in expanded state only otherwise its records are not accessed in QueryCellStyleInfo event. So that the customization of child table records was not exported to excel if it is in Collapsed state. 
 
If you want to export the nested table customization which is done in QueryCellStyleInfo event, you need to expand the nested tables before exporting. 
 
In the attached sample, the nested tables are expanded before exporting and collapsed after exported the grid to excel. 
Code example: 
if (saveFileDialog.ShowDialog() == DialogResult.OK) 
{ 
    this.gridGroupingControl1.Table.ExpandAllRecords(); 
    ExportToExcel(saveFileDialog.FileName); 
    Process.Start(saveFileDialog.FileName); 
    this.gridGroupingControl1.Table.CollapseAllRecords(); 
} 
 
Sample link: 
 
Regards, 
Piruthiviraj

Loader.
Live Chat Icon For mobile
Up arrow icon