简体   繁体   中英

How to bind a list of POCO objects to Windows Forms DataGrid

I've not really done any windows forms before, and I am attempting to create a simple app for me to view my message logging service.

I thought the simplest way to do this would be with a DataGridView control. How do you bind a simple list of objects to it and have the column headings etc auto generated?

I have done this so far and it creates the columns with headings but no rows/data.

List<Log> logs = loggingRepo.LoadAll();

logGrid.DataSource = logs;

I would have expected there to be a logGrid.DataBind() method like list views in ASP.NET but there doesn't seem to be one.

There's no databind, the setting of DataSource takes place immediately.

Binding to POCOs isn't great though, you end up with a less than optimal experience for the user. I'd wrap the logs collection in a BindingList, that should enable some basic sorting and filtering.

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