格式化radgrid导出为PDF

时间:2015-04-23 17:36:47

标签: pdf radgrid export-to-pdf

我有一个数据表dt,并希望从中制作PDF文件。一切都是动态的,它可以工作,但我不能格式化布局,如备用行背景等。这是我的代码:

   private void MakeGridExportToPDF(string strTitle, DataTable dt)
{
    using (RadGrid grid = new RadGrid { AutoGenerateColumns = false, ShowHeader = true, })
    {
        grid.NeedDataSource += (object sender, GridNeedDataSourceEventArgs e) =>
              {
                  grid.DataSource = dt;
              };
        // Add columns
        dt.Columns.OfType<DataColumn>().ToList().ForEach(col =>
        {
            grid.MasterTableView.Columns.Add(new GridBoundColumn { DataField = col.ColumnName, HeaderText = col.Caption.ToCamel() });
        });
        GridExportSettings export = grid.ExportSettings;
        export.OpenInNewWindow = true;
        export.FileName = strTitle;
        export.IgnorePaging = true;
        GridPdfSettings pdf = export.Pdf;
        pdf.PageHeight = Unit.Parse("210mm"); // 
        pdf.PageWidth = Unit.Parse(GetPageWidth(grid.MasterTableView.Columns.Count)); // 
        pdf.DefaultFontFamily = "Arial Unicode MS";
        pdf.PageTopMargin = Unit.Parse("45mm");
        grid.ItemCreated += (object sender, GridItemEventArgs e) =>
         {
             GridItem item = e.Item;
             if (item is GridDataItem)
             {
                 item.Style["vertical-align"] = "middle";
                 item.Style["text-align"] = "center";
             }
             switch (item.ItemType) //Mimic RadGrid appearance for the exported PDF file
             {
                 case GridItemType.Item:
                     item.Style["background-color"] = "#DFDFDF";
                     item.Font.Italic = true;
                     break;
                 case GridItemType.AlternatingItem:
                     item.Style["background-color"] = "#FFFFFF";

                     break;
                 case GridItemType.Header:
                     item.Style["background-color"] = "#FFFFFF";
                     item.Style["Color"] = "#767676";
                     item.Font.Bold = true;
                     break;
                 case GridItemType.Footer:
                     item.Style["background-color"] = "#FFFFFF";


                     break;
             }
         };

        this.smCSVFile.Controls.Add(grid);
        grid.MasterTableView.ExportToPdf();
        bDone = true;
    }
}

我做错了什么。在首先创建网格时触发ItemCreated,但在导出时不触发,并且看不到任何更改。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

我的问题已通过删除使用来解决(RadGrid网格....并且只是制作Radgrid = new RadGrid { AutoGenerateColumns = false, ShowHeader = true, }