110,537
社区成员
发帖
与我相关
我的任务
分享
if (tagRegistered == null)
{
tagNew.Isvalid = "Normal condition";
}
else ////找到了登记信息,说明异常,报警
{
tagNew.GoodsName = tagRegistered.GoodsName;
tagNew.Isvalid = "Abnormal condition";
this.BeginInvoke(new Action(() => { Alert(); }));
}
listTags.Add(tagNew);
}
}
if (newTags)
{
this.BeginInvoke(new Action(() => { BindData(); }));
}
}
/// <summary>
/// 绑定gridMonitor,设置格式
/// </summary>
private void BindData()
{
this.gridMonitor.AutoGenerateColumns = false;
//疑问:使用List绑定,list中的多条记录,在grid中只能显示1条
//this.gridMonitor.DataSource = listTags;
//转换成BindingList,据能正常显示所有行
this.gridMonitor.DataSource = new BindingList<TagInfoExtend>(listTags);
}
private List<TagInfoExtend> listTags = new List<TagInfoExtend>();
private BindingList<TagInfoExtend> bindingListTags = new BindingList<TagInfoExtend>(listTags );
//其它
this.gridMonitor.DataSource = bindingListTags ;
你这个无效是因为你的List根本没赋进去,如果你的List一直变化的话,这里的变化指的是引用地址没变,如果你通过xxx = yyy.ToList()这样是不能监控的,因为引用地址都变了,你要调用BindingList我不知道有没有refresh方法,但bindingsource是有的
你按上面的改动下试试看呢,你只要保证listTags 不会重新进行赋值就可以private IRFIDReader reader;
//private List<TagInfoExtend> listTags = new List<TagInfoExtend>(); //记录读取到的标签集合
private BindingList<TagInfoExtend> listTags = new BindingList<TagInfoExtend>(); //记录读取到的标签集合
private List<MW_TagDefine> listTagsRegistered; //将系统登记的所有标签预先载入,以提高判断时的速度
private string CorpId = ConfigurationManager.AppSettings["CorpId"];
public FormAntiMonitor()
{
InitializeComponent();
}
/// <summary>
/// 窗体载入
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FormAntiMonitor_Load(object sender, EventArgs e)
{
stripBtnStart.Enabled = true;
stripBtnStop.Enabled = false;
//取得读写器实例,并连接读写器
reader = RFIDReaderFactory.GetItemRFIDReader("B433001");
reader.TagsReportEvent += new TagsReportHandle(reader_TagsReportEvent);
//加载所有登记的标签
using (var context = new AMContext())
{
listTagsRegistered = context.MW_TagDefine.ToList();
}
//设置数据源
this.gridMonitor.AutoGenerateColumns = false;
this.gridMonitor.DataSource = listTags;
}
private static object locker = new object();
/// <summary>
/// 绑定gridMonitor,设置格式
/// </summary>
private void BindData()
{
//加了锁,以下代码运行正常!
lock (locker)
{
this.gridMonitor.AutoGenerateColumns = false;
//疑问:使用List绑定,list中的多条记录,在grid中只能显示1条
this.gridMonitor.DataSource = listTags;
//转换成BindingList,就能正常显示所有行
//this.gridMonitor.DataSource = new BindingList<TagInfoExtend>(listTags);
}
}