I have an Arraylist containing an amount of objects. 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):
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.
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.