简体   繁体   English

c#DataGrid绑定到列表

[英]c# DataGrid bound to List

I have a dataGrid bound to a List object and this works fine by calling... 我有一个绑定到List对象的dataGrid,可以通过调用...来正常工作。

dgList.DataSource = carList;

However I have code that updates the carList on background threads by polling servers and also deletes based on age. 但是我有代码通过轮询服务器来更新后台线程上的carList,并且还根据年龄删除了代码。 The datagrid does not seem to update at all, I tried calling .Update() and that has no effect. 数据网格似乎根本没有更新,我尝试调用.Update() ,但这没有任何效果。

Is this possible? 这可能吗?

The list is defined as 该列表定义为

List<Car> = carList = new List<Car>();

Refresh won't work because it only redraws the control: Refresh将不起作用,因为它仅重绘了控件:

Forces the control to invalidate its client area and immediately redraw itself and any child controls. 强制控件使它的客户区无效,并立即重绘自身和所有子控件。

The simplest solution is likely to rebind using DataSource again: 最简单的解决方案可能会再次使用DataSource重新绑定:

dgList.DataSource = carList;
carList.Add(car);
dgList.DataSource = null;
dgList.DataSource = carList;

You have to rebind the data with DataBind again 您必须再次将数据与DataBind绑定

as for WinForms: have you tried to reset the source again? 至于WinForms:您是否尝试过再次重置源? If not use a BindingSource instead of the raw list. 如果不是,请使用BindingSource而不是原始列表。

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

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