遇到datagridview更新的难题,请各位不吝指教

qzexplore 2006-09-28 02:58:24
步骤如下:
1、我用datagridview绑定datatable.
public void Update()
{
DataTable dataTable= GetDataTable();//从数据库获取数据
bindingSource1.DataSource = dataTable;
dataGridView1.DataSource = bindingSource1;
}

2、另外有其它工作线程删除dataTable中的某几行
public void Delete(int id)
{
DataRow[] rows = dataTable.Select("id=" + id);
if (rows == null || rows.Length == 0)
return;
dataTable.Rows.Remove(rows[0]);
}

3、删除后邮工作线程通知界面更新datagridview.
调用界面函数
infoUpdate()
{
if(InvokeRequired)
this.Invoke(new UpdateViewDelegate(infoUpdate));
else
Update();
}
其中的UpdateViewDelegate 代理重新调用 infoUpdate()

这个正常删除工作都正常,但是当删除到最后一行时,程序出现异常错误,错误如下:
在 System.Windows.Forms.DataGridViewRow.GetErrorText(Int32 rowIndex)
在 System.Windows.Forms.DataGridViewRow.Paint(Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, Int32 rowIndex, DataGridViewElementStates rowState, Boolean isFirstDisplayedRow, Boolean isLastVisibleRow)
在 System.Windows.Forms.DataGridView.PaintRows(Graphics g, Rectangle boundingRect, Rectangle clipRect, Boolean singleHorizontalBorderAdded)
在 System.Windows.Forms.DataGridView.PaintGrid(Graphics g, Rectangle gridBounds, Rectangle clipRect, Boolean singleVerticalBorderAdded, Boolean singleHorizontalBorderAdded)
在 System.Windows.Forms.DataGridView.OnPaint(PaintEventArgs e)
在 System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
在 System.Windows.Forms.Control.WmPaint(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(Int32 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)
在 MyApp.Program.Main()
在 System.AppDomain.nExecuteAssembly(Assembly 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)
在 System.Threading.ThreadHelper.ThreadStart()

请问各位,这是什么原因??
...全文
359 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
qzexplore 2006-09-30
  • 打赏
  • 举报
回复
问题终于解决了,TNND,datagridview居然搞出这种问题出来
只要把datagridview 启用添加去掉,添加启用编辑、启用删除功能(偶不在datagridview编辑数据)。耗了我好多时间
qzexplore 2006-09-30
  • 打赏
  • 举报
回复
kongxiangli(笑看红尘)
你认为怎么才能解除耦合?

qzexplore 2006-09-29
  • 打赏
  • 举报
回复
删除方法是另一个线程调用的。
changlongbaobao 2006-09-29
  • 打赏
  • 举报
回复
c#讨论群30781666 欢迎有经验的高手加入
凋零的老树 2006-09-29
  • 打赏
  • 举报
回复
DataTable dataTable= GetDataTable().Copy();//从数据库获取数据
bindingSource1.DataSource = dataTable;
dataGridView1.DataSource = bindingSource1;


数据的偶合性太强了,后面处理麻烦
qzexplore 2006-09-29
  • 打赏
  • 举报
回复
没人,自己顶!
zfc1978 2006-09-28
  • 打赏
  • 举报
回复
你上面的代码应该没错,就是看调用那个删除方法的代码怎么写的?
qzexplore 2006-09-28
  • 打赏
  • 举报
回复
目前只好用最笨方法:
public void Update()
{
DataTable dataTable= GetDataTable().Copy();//从数据库获取数据
bindingSource1.DataSource = dataTable;
dataGridView1.DataSource = bindingSource1;
}
希望高手们能指点
  • 打赏
  • 举报
回复
我顶
qzexplore 2006-09-28
  • 打赏
  • 举报
回复
pinglan() :不是那个问题,就是剩下最后一行,再删除,datatable也能正常删除.但是在datagraidview显示出现异常.
谢谢各位热心帮助
myminimouse 2006-09-28
  • 打赏
  • 举报
回复
帮顶
pinglan 2006-09-28
  • 打赏
  • 举报
回复
public void Delete(int id)
{
DataRow[] rows = dataTable.Select("id=" + id);
if (rows == null || rows.Length == 0)
return;
dataTable.Rows.Remove(rows[0]);
}
//可否改成
public void Delete(int id)
{
if (rows == null || rows.Length == 0)
return;
DataRow[] rows = dataTable.Select("id=" + id);

dataTable.Rows.Remove(rows[0]);
}
//............
yzqlee 2006-09-28
  • 打赏
  • 举报
回复
up
zzd8310 2006-09-28
  • 打赏
  • 举报
回复
帮顶
qzexplore 2006-09-28
  • 打赏
  • 举报
回复
当rows 的length=0 便会报错
xlshen_lxz 2006-09-28
  • 打赏
  • 举报
回复
跟踪看看rows的length是多少?是否有数据

110,536

社区成员

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

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

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