简体   繁体   English

如何将数组列表作为数据源添加到devexpress gridcontrol

[英]How to add an arraylist as datasource to devexpress gridcontrol

I have an Arraylist containing an amount of objects. 我有一个包含大量对象的Arraylist。 All these Objects have a name, size... Now I want every object to be shown as a row in a grid control. 所有这些对象都有一个名称,大小...现在,我希望每个对象在网格控件中显示为一行。 If I write: 如果我写:

gridControl.DataSource = ArrayList;

I get the right amount of rows, but without filling. 我得到适当数量的行,但没有填充。 How can I add the Values of each objects attributes? 如何添加每个对象属性的值?

ArrayList dataSource = new ArrayList();
dataSource.Clear();

foreach(FileInfo element in dir.GetFiles())
{           
     dataSourceEntry item = new dataSourceEntry();
     item.fileCreateDate = element.CreationTime.Date;
     item.fileName = element.Name;
     item.check = true;
     dataSource.Add(item);
}
gridFiles.DataSource = dataSource;

Because it is hard to extract column's information from ArrayList , I suggest you use generic List<T> instead (or BindingList<T> if you want to track element's changes): 因为很难从ArrayList提取列的信息,所以建议您改用通用的List<T> (如果要跟踪元素的更改,则使用BindingList<T> ):

List<dataSourceEntry> dataSource = new List<dataSourceEntry>();

foreach(FileInfo element in dir.GetFiles())
{           
     dataSourceEntry item = new dataSourceEntry();
     item.fileCreateDate = element.CreationTime.Date;
     item.fileName = element.Name;
     item.check = true;
     dataSource.Add(item);
}
gridFiles.DataSource = dataSource;
gridFiles.MainView.PopulateColumns();

PS Anyway, your approach with ArrayList should work correctly when the data source assigned to grid is not empty. PS无论如何,当分配给grid的数据源不为空时,使用ArrayList的方法应该可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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