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

GROUP CELL BACKGROUND COLOR CHANGE FOR EVERY GROUP/SUBGROUP AND COLUMN NAME BEFORE GROUP FIELD VALUE - EXPORT TO EXCEL /PDF

Hi,

In SFDATAGRID 

1. How to change backgound colour of Cell for Group/Subgroup.. for Export to Excel as well as Export to PDF

Example :

If In Drag Group area If we have selected 5 Fields.. Col A, Col B, Col C, Col D and Col E

In Grid Data Area How Background Cell Color for A Field Groupwise = A Color, for B Field Groupwise (Subgroup) = B Color, for C Field Groupwise (Subgroup of B) = C Colour... on and on..

Please check the attached Excel Sheet with Current Result and Expected Result in REQUIRED RESULT-1 sheet

2. In Group result, field name with Field value of selected field for Grouping is required. 

Please check the attached Excel Sheet with Current Result and Expected Result in REQUIRED RESULT-2 sheet.

Please note that  both the result required while export to Excel and Export to PDF also.

Hope that you will understand and do the needful.




Attachment: bank_master_demo_4b85841a.rar

1 Reply

JN Jayaleshwari N Syncfusion Team January 11, 2019 12:15 PM UTC

Hi Deepak, 
 
We have checked the reported queries. Please find the details as following. 
 
Queries 
Details 
Set background color to each group based on the level in grid 
You can achieve your requirement by using DrawCell eventby checking the RowType as CaptionCoveredRow 
 
private void SfDataGrid1_DrawCell(object sender, DrawCellEventArgs e) 
{ 
    if (e.DataRow.RowType == RowType.CaptionCoveredRow) 
    { 
        var level = (e.DataRow.RowData as Group).Level; 
        if (level == 1) 
            e.Style.BackColor = Color.YellowGreen; 
        else if (level == 2) 
            e.Style.BackColor = Color.AliceBlue; 
        else if (level == 3) 
            e.Style.BackColor = Color.IndianRed; 
    } 
} 
 
You can refer the following user guide documentation to customize the caption summary cell. 
In Group result, field name with Field value of selected field for Grouping is required.  
By default our SfDataGrid displays the group header with column name along with summary value. 
 
You can refer the following screen shot. 
 
How to change backgound colour of Cell for Group/Subgroup.. for Export to Excel as well as Export to PDF 
You can achieve this requirement by customizing the converter. You can refer the following code snippets. 
 
Exporting to excel. 
public class SfDataGridExcelConvereter    : DataGridToExcelConverter 
{ 
    int level = -1; 
    protected override void ExportGroupCaptionToExcel(SfDataGrid grid, ICollectionViewAdv view, IWorksheet sheet, ExcelExportingOptions excelExportingOptions, Group group) 
    { 
        level = group.Level; 
        base.ExportGroupCaptionToExcel(grid, view, sheet, excelExportingOptions, group); 
    } 
 
    protected override void ExportSummaryCellToExcel(SfDataGrid grid, IRange summaryRange, ExportCellType exportCellType, string summaryDisplayText, object exportNodeEntry, string columnName, ExcelExportingOptions excelExportingOptions) 
    { 
        base.ExportSummaryCellToExcel(grid, summaryRange, exportCellType, summaryDisplayText, exportNodeEntry, columnName, excelExportingOptions); 
        if (level == 1) 
            summaryRange.CellStyle.FillBackground = ExcelKnownColors.Green; 
        else if (level == 2) 
            summaryRange.CellStyle.FillBackground = ExcelKnownColors.Blue; 
        else if (level == 3) 
            summaryRange.CellStyle.FillBackground = ExcelKnownColors.Red; 
        level = -1; 
    } 
} 
 
private void ExportToExcel_Click(object sender, EventArgs e) 
{ 
    var options = new ExcelExportingOptions(); 
    var excelConverter = new SfDataGridExcelConvereter(); 
    var excelEngine = excelConverter.ExportToExcel(sfDataGrid1, sfDataGrid1.View, options);             
    var workBook = excelEngine.Excel.Workbooks[0]; 
    workBook.SaveAs("Sample.xlsx"); 
} 
 
 
Exporting to PDF 
 
public class SfDataGridPDFConvereter    : DataGridToPdfConverter 
{ 
    int level = -1; 
    protected override void ExportGroupCaptionToPdf(SfDataGrid sfgrid, PdfGrid pdfGrid, ICollectionViewAdv view, PdfExportingOptions pdfExportingOptions, Group group) 
    { 
        level = group.Level; 
        base.ExportGroupCaptionToPdf(sfgrid, pdfGrid, view, pdfExportingOptions, group);             
    } 
         
    protected override void ExportSummaryCellToPdf(SfDataGrid sfGrid, PdfGridCell pdfGridCell, PdfExportingOptions pdfExportingOptions, ExportCellType exportCellType, string summaryDisplayText, object exportNodeEntry, string columnName) 
    {             
        base.ExportSummaryCellToPdf(sfGrid, pdfGridCell, pdfExportingOptions, exportCellType, summaryDisplayText, exportNodeEntry, columnName); 
        if (level == 1) 
            pdfGridCell.Style.BackgroundBrush = PdfBrushes.YellowGreen; 
        else if (level == 2) 
            pdfGridCell.Style.BackgroundBrush = PdfBrushes.AliceBlue; 
        else if (level == 3) 
            pdfGridCell.Style.BackgroundBrush = PdfBrushes.IndianRed; 
        level = -1; 
    } 
} 
 
private void ExportToPDF_Click(object sender, EventArgs e) 
{ 
    PdfExportingOptions options = new PdfExportingOptions();            
    var pdfConverter = new SfDataGridPDFConvereter(); 
    var document = pdfConverter.ExportToPdf(sfDataGrid1,options);                       
    document.Save("Sample.pdf"); 
}     
 
 
We have attached the sample for your reference and it is available in the following location.  
 
Please let us know if you would require further assistance. 
 
Regards, 
Jayaleshwari N. 


Loader.
Live Chat Icon For mobile
Up arrow icon