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

[Solved] RadGrid Export to Excel apply a background based on the value of another column

3 Answers 207 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Content and Code
Top achievements
Rank 1
Content and Code asked on 16 Sep 2009, 11:23 AM
Hi,

I have a rad grid in which I display some data. There is a coloumn (which is not shown in the grid itself) which I need to use to colour the background of the row. So if it has a certain value then I need to make the background for that record to be red when the data is exported to excel.

I have searched through the forums and I have searched online and seen the demo's. However I woudl appreciate if someone could give me some pointers on this.

3 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 16 Sep 2009, 05:00 PM
Hello Tobias,

A runnable sample is shown below:
<asp:Button ID="Button1" runat="server" Text="Export" OnClick="Button1_Click" /> 
<telerik:RadGrid ID="RadGrid1" runat="server"  
    OnNeedDataSource="RadGrid1_NeedDataSource"  
    onitemdatabound="RadGrid1_ItemDataBound"
    <MasterTableView AutoGenerateColumns="false"
        <Columns> 
            <telerik:GridBoundColumn DataField="ID" Display="false" /> 
            <telerik:GridBoundColumn DataField="Name" /> 
        </Columns> 
    </MasterTableView> 
</telerik:RadGrid> 

dummy datasource
protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e) 
    DataTable table = new DataTable(); 
    table.Columns.Add("ID"typeof(int)); 
    table.Columns.Add("Name"); 
    for (int i = 0; i < 10; i++) 
        table.Rows.Add(i, "Name" + i); 
    RadGrid1.DataSource = table; 

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    if (e.Item is GridDataItem) 
    { 
        int id = (int)DataBinder.Eval(e.Item.DataItem, "ID"); 
        if (id == 3) 
            e.Item.Style["background-color"] = "#4455AC"
    } 

protected void Button1_Click(object sender, EventArgs e) 
    RadGrid1.MasterTableView.ExportToPdf(); 

I hope this helps

Best regards,
Daniel
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Content and Code
Top achievements
Rank 1
answered on 16 Sep 2009, 10:26 PM
Thanks for the reply Daniel.

This works but it also applies the bakground to the rows on the radGrid itself. I dont want to apply this logic to the grid itself but only to the data that is exported. So in other words I want the grid to appear as normal but when the data is exported I want the relevant rows in the .xls file to have this colour background based on the value in these specific column.
0
Daniel
Telerik team
answered on 17 Sep 2009, 10:46 AM
Hello Tobias,

There are two ways to achieve this - to add a flag to distinguish when to apply the custom style or to use the ExcelExportCellFormatting event:
protected void RadGrid1_ExcelExportCellFormatting(object source, ExcelExportCellFormattingEventArgs e) 
    if (e.Cell.NamingContainer is GridDataItem) 
    { 
        GridDataItem item = e.Cell.NamingContainer as GridDataItem; 
        int id = int.Parse(item["ID"].Text); 
        if (id == 3) 
            item.Style["background-color"] = "#4455AC"
    } 

Best regards,
Daniel
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Grid
Asked by
Content and Code
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Content and Code
Top achievements
Rank 1
Share this question
or