简体   繁体   中英

problems with autowidth for columns of a grid control from devexpress

I'am using a grid control from devexpress in a desktop application(C#). I set a datasource and then I export to Excel.I want that all my columns take the width of longest row value from that column. In this grid I use different datasources .

In load form I set :

gvExport.OptionsPrint.AutoWidth = false;
gvExport.BestFitColumns();
grdExport.DataSource = ds;

XlsExportOptions vOptions = new XlsExportOptions();
vOptions.TextExportMode = TextExportMode.Text;
vOptions.ShowGridLines = true;
vOptions.SheetName = "Test";

prmFileName = "Test.xls";

grdExport.ExportToXls(prmFileName, vOptions);

My ds can be a list or a dataTable.

1.Can somebody hepl me to autosize the lenght of columns?

2.How can I set to ladscape the Excel page that I generated?

Thanks

For the first part of the question you can use

GridView.OptionsView.ColumnAutoWidth = true

if you want to set the width according to a particular column, let say the first column you have to inheret the GridView and then override the functions BestFitColumn and OnColumnWidthChanged.

For the second part you have to use the printing system like this:

        PrintableComponentLink link = new PrintableComponentLink(new PrintingSystem());
        link.PaperKind = System.Drawing.Printing.PaperKind.A4;
        link.Component = myGridControl;
        link.Landscape = true;

and then use an ExportOption, here an example of Xls:

 XlsExportOptions _Options1 = new XlsExportOptions();
 _Options1.SheetName = fileName;
 _Options1.ExportMode = XlsExportMode.SingleFile;
 link.ExportToXls(sfd.FileName, _Options1);

Note for others there is XlsxExportOptions , PdfExportOptions , RtfExportOptions ...etc

Tou should know that the BestFitColumns() method iterates on the rows of the data source to compute the best width for the columns.

So, you have to put it AFTER setting your data source.

Therefore, I recommend you not to use this method and set manually the width of the columns instead. In fact, the BestFitColumns() method slows considerably the loading of your grid (if you are dealing with >1k rows).

For Exporting to a landscape mode, you'd probably find an answer here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM