步骤如下:
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()请问各位,这是什么原因??
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()请问各位,这是什么原因??
解决方案 »
- 希望能在propertygrid (C#)字段中添加自定义异常提示信息
- Dataset使用问题
- C# 两个库对比 请高手指点
- 如何激活应用程序并传递参数
- VS2010编写的winform程序,局域网安装完成之后运行不了,求帮助啊
- 谁有json的C#实现代码,实现下面的php代码里的功能就可以,附PHP的Json实现代码,支持Json的顶一下!
- 請問DataSet.DataTable.Select()如何實現多個字段排序?
- 拖动treeView的结点到文本框怎么实现/
- 如何在WinForm中对TextBox的值进行校验
- 怎样把菜单放大
- windows应用程序中怎么实现datagride中添加button控件(急)
- 高份求教代码:如何在上传时将文本文件的空行去除掉?
{
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]);
}
//............
谢谢各位热心帮助
public void Update()
{
DataTable dataTable= GetDataTable().Copy();//从数据库获取数据
bindingSource1.DataSource = dataTable;
dataGridView1.DataSource = bindingSource1;
}
希望高手们能指点
bindingSource1.DataSource = dataTable;
dataGridView1.DataSource = bindingSource1;
数据的偶合性太强了,后面处理麻烦
你认为怎么才能解除耦合?
只要把datagridview 启用添加去掉,添加启用编辑、启用删除功能(偶不在datagridview编辑数据)。耗了我好多时间