This is a migrated thread and some comments may be shown as answers.

aggregate gridview cells

3 Answers 81 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Alden
Top achievements
Rank 1
Alden asked on 20 Oct 2015, 07:06 AM

Hi, I am currently using the trial version of Telerik Winform and I would like a code example on how to aggregate cells in my gridview with the following two examples.

 Example1:

1997     a                                           a

1997     b  ->  becomes  ->   1997     b

1997     c                                            c

 Where 1997 becomes a vertical aggregate field.

Example2:

1997     1997     1997 ->               1997

a            b           c               a          b          c

Where 1997 becomes a horizontal aggregate field

3 Answers, 1 is accepted

Sort by
0
Hristo
Telerik team
answered on 22 Oct 2015, 06:39 AM
Hello Alden,

Thank you for writing.

You can achieve the desired result by handling the CellFormatting event of your RadGridView and for certain cells having set their Text property to an empty string. Additionally you could also set their DrawBorder property to false. Please check my code snippet below: 
private void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
{
    if (e.CellElement.RowIndex > 0 && e.CellElement.ColumnInfo.Name == "1997")
    {
        e.CellElement.Text = "";
        e.CellElement.DrawBorder = false;
    }
}

More information is available in the following documentation article: Formatting Cells. Additionally I am sending you a screenshot of the result on my end.

I hope this helps. Should you have further questions please do not hesitate to write back.

Regards,
Hristo Merdjanov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Alden
Top achievements
Rank 1
answered on 23 Oct 2015, 03:04 PM
Hi Hristo, thanks a lot for the reply! I meant to have that aggregated output for the Excel export. Since the Telerik Excel export accepts a radgridview I thought I need to aggregate it before I can export it like the Pivotgrid. But anyway, does the result for your radgridview aggregate carries on to the Excel export? Thanks again!
0
Hristo
Telerik team
answered on 28 Oct 2015, 11:16 AM
Hello Alden,

Thank you for writing back.

The solution I last sent you concerns the visual appearance in RadGridView and exporting it as is will not aggregate the results in Excel. In order to achieve this task, you would need to handle the WorkBookCreated event of your SpreadExportRenderer instance. Then thanks to our SpreadProcessing library you can merge the cells having equal value of your column. Please check my snippet below: 
private void radButton1_Click(object sender, EventArgs e)
{
    GridViewSpreadExport spreadExporter = new GridViewSpreadExport(this.radGridView1);
    spreadExporter.ExportVisualSettings = true;
    SpreadExportRenderer exportRenderer = new SpreadExportRenderer();
    exportRenderer.WorkbookCreated += exportRenderer_WorkbookCreated;
    spreadExporter.RunExport(@"..\..\\exportedFile.xlsx", exportRenderer);
}
 
private void exportRenderer_WorkbookCreated(object sender, WorkbookCreatedEventArgs e)
{
    CellIndex start = new CellIndex(1, this.radGridView1.Columns.IndexOf("1997"));
    CellIndex end = new CellIndex(this.radGridView1.Rows.Count, this.radGridView1.Columns.IndexOf("1997"));
 
    (e.Workbook.Sheets[0] as Worksheet).Cells[start, end].Merge();
}

More information is available in the following documentation resources: RadGridView | SpreadExport, SpreadProcessing | Merge and Unmerge Cells.

I hope this helps. Should you have further questions please do not hesitate to write back. 

Regards,
Hristo Merdjanov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
GridView
Asked by
Alden
Top achievements
Rank 1
Answers by
Hristo
Telerik team
Alden
Top achievements
Rank 1
Share this question
or