BindingSource 不能是自己的数据源。请不要将 DataSource 和 DataMember 属性设置为循环引用 BindingSource 的值。

WoooXVi 2011-02-14 06:37:31
C#的DataGridView绑数据表
表被另外一条线程修改。我界面只要要显示这个表的数据,但是数据变动很频繁,而且界面的控件要根据数据表的内容,做一些变动。
请问如何绑定才保险?

我现在的办法是
微软封装的数据表:dataTableMain
绑定控件:bindingSourceMain
DataGridView:dataGridViewMain



bindingSourceMain.DataSource = dataTableMain;
dataGridViewMain.DataSource = bindingSourceMain;
dataTableMain.RowChanged += new DataRowChangeEventHandler(dataTableMain_RowChanged);
dataTableMain.RowDeleted += new DataRowChangeEventHandler(dataTableMain_RowChanged);


另外一条线程修改的是dataTableMain……

跑着跑着,经常会出现问题。而且错误停在程序入口处:Application.Run那
错误信息是:
BindingSource 不能是自己的数据源。请不要将 DataSource 和 DataMember 属性设置为循环引用 BindingSource 的值。

堆栈无显示
线程也无显示……杯具

未处理 System.InvalidOperationException
Message=BindingSource 不能是自己的数据源。请不要将 DataSource 和 DataMember 属性设置为循环引用 BindingSource 的值。
Source=System.Windows.Forms
StackTrace:
在 System.Windows.Forms.BindingSource.get_Count()
在 System.Windows.Forms.CurrencyManager.get_Count()
在 System.Windows.Forms.DataGridViewCell.GetValue(Int32 rowIndex)
在 System.Windows.Forms.DataGridViewTextBoxCell.GetPreferredSize(Graphics graphics, DataGridViewCellStyle cellStyle, Int32 rowIndex, Size constraintSize)
在 System.Windows.Forms.DataGridViewCell.GetPreferredWidth(Int32 rowIndex, Int32 height)
在 System.Windows.Forms.DataGridViewCell.OnCellDataAreaMouseEnterInternal(Int32 rowIndex)
在 System.Windows.Forms.DataGridViewCell.OnMouseMoveInternal(DataGridViewCellMouseEventArgs e)
在 System.Windows.Forms.DataGridView.OnCellMouseMove(DataGridViewCellMouseEventArgs e)
在 System.Windows.Forms.DataGridView.UpdateMouseEnteredCell(HitTestInfo hti, MouseEventArgs e)
在 System.Windows.Forms.DataGridView.OnMouseMove(MouseEventArgs e)
在 System.Windows.Forms.Control.WmMouseMove(Message& m)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.DataGridView.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.Run(Form mainForm)
在 LD.Future200.UI.Program.Main(String[] param) 位置 D:\shihao\workdir\07.Future200\04.Src\15.UI\Program.cs:行号 30
在 System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()
InnerException:
...全文
1367 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
WoooXVi 2011-12-28
  • 打赏
  • 举报
回复
希望对你有用。我现在自己用这个:

http://www.xwooo.com/ArticleView_390.html
WoooXVi 2011-12-28
  • 打赏
  • 举报
回复
Invoke到UI线程后再修改要绑定到DataGridView.DataSource的数据表。
RegenHo 2011-12-22
  • 打赏
  • 举报
回复
问题解决了么?我也遇到了相同的问题,帮忙解决一下!
WoooXVi 2011-02-17
  • 打赏
  • 举报
回复
Help……
WoooXVi 2011-02-15
  • 打赏
  • 举报
回复
而且问题还是没解决,还是出现……郁闷呀……
WoooXVi 2011-02-15
  • 打赏
  • 举报
回复
谢谢楼上的“Bing”
现在又有新问题了

我窗口关闭的时候会出现“无法访问已释放的对象……”

经过调查,怀疑是相应另外一个线程的修改的过程中,窗口被关闭了!

所以我就加了个旗标,并在修改和读取的时候锁住。在窗口关闭的时候,旗标置为false。
在相应事件的时候,先判断旗标再执行相应代码……

结果这样会出现死锁……

请问如何解决?
yalan 2011-02-14
  • 打赏
  • 举报
回复
我是来拜如梦mm的,顺便帮顶
wuyq11 2011-02-14
  • 打赏
  • 举报
回复
加个delegate,然后用invoke调用databind
取数据前先要清除的数据绑定,再取数据并重新绑定。

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧