GridViewDataColumn[] grdColumn = new GridViewDataColumn[ds.Tables[0].Columns.Count];
foreach (DataColumn dColumn in ds.Tables[0].Columns)
{
GridViewTextBoxColumn grdCol = new GridViewTextBoxColumn();
grdCol.FieldName = dColumn.Caption;
grdCol.HeaderText = dColumn.Caption;
string DisplayFormat = SchB.Get_DisplayFormat(ReportID, dColumn.Caption);
string TempDF = string.Empty;
if (string.Compare(Convert.ToString("" + dColumn.DataType), "System.String", true) == 0)
{
grdCol.TextAlignment = ContentAlignment.MiddleLeft;
grdCol.FormatString = "{0:0}";
}
else if (string.Compare(Convert.ToString("" + dColumn.DataType), "System.Decimal", true) == 0)
{
grdCol.TextAlignment = ContentAlignment.MiddleRight;
}
else if (string.Compare(Convert.ToString("" + dColumn.DataType), "System.DateTime", true) == 0)
{
grdCol.TextAlignment = ContentAlignment.MiddleRight;
grdCol.FormatString = "{0:dd/MM/yyyy}";
}
else
{
grdCol.TextAlignment = ContentAlignment.MiddleCenter;
}
grdColumn[tempCount++] = grdCol;
}
gridUserReport.Columns.AddRange(grdColumn);
Regards,
Sakthivel
16 Answers, 1 is accepted
I have smiler problem, i could not format the dynamic column using code
I didn't receive any error message,
But it shows the data without format
Any help..,
with regards
Jamesraj,
It looks like there is an issue with transferring the text alignment from grid to exported pdf file. We will address this in one of the next releases. For the time being you can manually set desired alignment or apply any additional format through HTMLCellFormatting event:
void
pdfExporter_HTMLCellFormatting(
object
sender, Telerik.WinControls.UI.Export.HTML.HTMLCellFormattingEventArgs e)
{
if
(e.GridCellInfo.ColumnInfo.Name ==
"City"
)
{
e.HTMLCellElement.Alignment = ContentAlignment.MiddleRight;
}
}
Let me know if there are any other additional questions.
Best wishes,Martin Vasilev
the Telerik team
Thanks for your reply,
With your idea alignment problem is solved,
how can i format the cell when exporting,
I already format the columns , it shows fine in grid
while exporting to PDF it does not show the format,
for example,
i have to format the datetime column as "dd/MM/yyyy" , etc
with regards
Jamesraj
Thank you for getting back to me.
Actually, you can use the same event to apply any formatting while exporting to pdf:
void
exportPdf_HTMLCellFormatting(
object
sender, Telerik.WinControls.UI.Export.HTML.HTMLCellFormattingEventArgs e)
{
if
(e.GridCellInfo.ColumnInfo.Name ==
"Date"
&& e.GridCellInfo.Value
is
DateTime)
{
e.HTMLCellElement.Value = ((DateTime)e.GridCellInfo.Value).ToString(
"dd MMM, yyyy"
);
}
}
Do not hesitate to contact me again if you have any other questions.
Best wishes,Martin Vasilev
the Telerik team
Thanks for reply, Its working fine
With Regards,
Jamesraj
Hi all,
I have Issue when I Grid export to XL or pdf in windows.10 with max Resolution (1920*1080).
The issues are here.
- The maximized Mdi or form is shrink to normal size automatically.
- Disorder the controls alignment.
The Issue is coming from the RunExport() function
Here my code is "exporter.RunExport(filename, exportRenderer);"
I attached two pics here, first one is before export Mdi picture and other one after export
How could I overcome the issue
With Regards,
Jamsheer
Following the provided information, I was unable to reproduce the issue you are facing. I have attached my sample project. Am I missing something? Could you please specify the exact steps how to reproduce the problem?
Alternatively, feel free to submit a support ticket from your account where you can provide a sample project demonstrating the problem you are facing. Thus, our support staff will gladly assist you further. Thank you in advance.
I am looking forward to your reply.
Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik
The provided gif file is greatly appreciated. However, it is not enough for me to replicate the issue locally. Have you tested the export behavior with the provided sample project from my previous reply? Is the issue reproducible with it? What is the Telerik UИ for WinForms version that you are currently using?
I am looking forward to your reply.
Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik
Hi Dess,
Thanks for your fast replay.
I tested with your code provided sample project from your previous reply, But its not solved my issue.
I am using "Visual Studio 2015".
Here is my code I used in my project.
public Form1()
{
InitializeComponent();
DataTable table = new DataTable();
table.Columns.Add("Id", typeof(int));
for (int i = 0; i < 10; i++)
{
table.Columns.Add("Column " + i, typeof(string));
}
for (int i = 0; i < 100; i++)
{
DataRow row = null;
row = table.NewRow();
row["Id"] = i + 1;
for (int j = 0; j < table.Columns.Count - 1; j++)
{
row["Column " + j] = "Name " + j * i;
}
table.Rows.Add(row);
}
this.radGridView1.DataSource = table;
this.radGridView1.Rows[3].IsPinned = true;
this.radGridView1.Columns[2].IsPinned = true;
this.radGridView1.Columns[2].PinPosition = Telerik.WinControls.UI.PinnedColumnPosition.Left;
}
private void radButton1_Click(object sender, EventArgs e)
{
SaveFileDialog dialog = new SaveFileDialog();
dialog.FileName = "exportedFile";
dialog.Filter = "xlsx files (*.xlsx)|*.xlsx|All files (*.*)|*.*";
if (dialog.ShowDialog() == DialogResult.OK)
{
ExportData(dialog.FileName);
}
}
void ExportData(string fileName)
{
SpreadExportRenderer renderer = new SpreadExportRenderer();
GridViewSpreadExport spreadExporter = new GridViewSpreadExport(this.radGridView1);
spreadExporter.FreezeHeaderRow = true;
spreadExporter.FreezePinnedColumns = true;
spreadExporter.FreezePinnedRows = true;
spreadExporter.ExportVisualSettings = true;
spreadExporter.SheetMaxRows = ExcelMaxRows._1048576;
spreadExporter.ExportFormat = SpreadExportFormat.Xlsx;
spreadExporter.FileExportMode = FileExportMode.CreateOrOverrideFile;
spreadExporter.RunExport(fileName, renderer);
}
Can I attach my project zip file here? If yes, how could I do
This issue is in Windows10 with maximum resolution(1980*1080)
Thank you
Jamsheer
I have tested my sample project with resolution 1920 x 1080 and it behaves as expected. After creating a new form with the provided code snippet, the problem is still not reproducible on my end. It seems to be something specific in the environment that you are using.
The allowed extensions in the forum threads are: .gif, .jpg, .jpeg, .png. That is why I kindly asked you to submit a ticket from your account where you can provide a full description of the precise case together with your sample project. Thus, our support staff will gladly will be able to make an adequate analysis of the precise case and assist you further. Thank you in advance.
Thank you for your understanding.
Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik
Hi Dess,
I run the project that you specified above, But there also getting the same issue.
The issue is coming only in "Windows 10 OS, Resolution (1920 x 1080)".
You should check with Windows 10 OS, Resolution (1920 x 1080)
I attached the gif file with run you specified project.
Thank you.
Jamsheer
The provided gif file was very useful since it clearly shows how th font was changed after exporting the grid. I was able to replicate the problem if the application is run on higher than 100% DPI scaling. Note that the grid export uses the RadSpreadProcessing library. Telerik’s document processing library (RadSpreadProcessing, RadWordProecessing or RadPdfProcessing) is referencing assemblies which are used in WPF. All WPF-based applications are DPI-aware by default and this is declared in the manifests of the WPF assemblies. Therefore, if you use the document processing library in WinForms applications that are not DPI-aware, they might suddenly become DPI-aware at run time when you instantiate a type from the DPL assemblies (when the DPL assemblies are loaded by the CLR, this will also load the WPF assemblies which they depend on, which in turn will make the application DPI-aware). If you intend to use your application on machines where the DPI scaling is larger than 100 percent, you should explicitly set the application to be DPI-unaware right before running the export logic:
private
void
workbookTestButton_Click(
object
sender, EventArgs e)
{
SetProcessDpiAwareness(_Process_DPI_Awareness.Process_DPI_Unaware);
Workbook wb =
new
Workbook();
}
[DllImport(
"shcore.dll"
)]
static
extern
int
SetProcessDpiAwareness(_Process_DPI_Awareness value);
enum
_Process_DPI_Awareness
{
Process_DPI_Unaware = 0,
Process_System_DPI_Aware = 1,
Process_Per_Monitor_DPI_Aware = 2
}
I have tested it on my end and it seems to work properly.
I hope this information helps.
Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik
Thank you, Its worked properly.
Jamsheer
Thank you!
I ran into the same problem today and your post fixed my error.