动态创建gridview,导出Excel数据
本帖最后由 1002068421 于 2017-6-21 13:51 编辑先创建一个GridControl,在创建一个GridView视图,再传入数据
GridControl CreateGrid(List<marketinformation> pagemodel)
{
GridControl grid = new GridControl(); ;
GridView view = new GridView();
grid.ViewCollection.Add(view);
grid.MainView = view;
view.GridControl = grid;
foreach (GridColumn column in gridView2.Columns)
{
DevExpress.XtraGrid.Columns.GridColumn gridColumnNumber = view.Columns.AddVisible(column.FieldName.ToString());
gridColumnNumber.Caption = column.Caption.ToString();
gridColumnNumber.FieldName = column.FieldName.ToString();
}
grid.DataSource = pagemodel;
this.Controls.Add(grid);//重要
grid.ForceInitialize();//重要
BestColumns(view);
view.OptionsPrint.AutoWidth = false;
return grid;
}
然后数据分页创建
IPrintable[] arr(List<marketinformation> data)
{
List<IPrintable> Printable=new List<IPrintable>();
int PageIndex=0;
int PageSize=50000;
int CurrentIndex=0;
int TotalCount=data.Count;
for(PageIndex=0;CurrentIndex<TotalCount;PageIndex++)
{
var PageData = data.Skip(PageIndex*PageSize).Take(PageSize).ToList();
Printable.Add(CreateGrid(PageData));
CurrentIndex=(PageIndex+1)*PageSize;
}
return Printable.ToArray();
}
最后数据多的时候自动列宽或假死,可以循环固定列宽,也可以写字段名
void BestColumns(GridView view)
{
if (chkeds.Checked)
{
view.OptionsView.ColumnAutoWidth = false;
foreach (GridColumn column in view.Columns)
{
if (column.Visible)
{
switch (column.FieldName.ToString())
{
case "caccount":
view.Columns["caccount"].Width = 150;
break;
case "cunitname":
view.Columns["cunitname"].Width = 300;
break;
case "ddate":
view.Columns["ddate"].Width = 150;
break;
case "cemskind":
view.Columns["cemskind"].Width = 100;
break;
case "cnum":
view.Columns["cnum"].Width = 150;
break;
case "dsysdate":
view.Columns["dsysdate"].Width = 150;
break;
default:
view.Columns.Width = 100;
break;
}
}
}
}
else
{
view.OptionsView.ColumnAutoWidth = true;
foreach (GridColumn column in view.Columns)
{
if (column.Visible)
{
view.Columns.Width = 75;
}
}
}
}
看看是什么黑科技{:2_31:} 谢谢楼主无私分享 谢谢分享
页:
[1]