步骤如下:
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.   

    跟踪看看rows的length是多少?是否有数据
      

  2.   

    当rows 的length=0 便会报错
      

  3.   

    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]);
    }
    //............
      

  4.   

    pinglan() :不是那个问题,就是剩下最后一行,再删除,datatable也能正常删除.但是在datagraidview显示出现异常.
    谢谢各位热心帮助
      

  5.   

    目前只好用最笨方法:
    public void Update()
    {
       DataTable dataTable= GetDataTable().Copy();//从数据库获取数据
       bindingSource1.DataSource = dataTable;
       dataGridView1.DataSource = bindingSource1;
    }
    希望高手们能指点
      

  6.   

    DataTable dataTable= GetDataTable().Copy();//从数据库获取数据
       bindingSource1.DataSource = dataTable;
       dataGridView1.DataSource = bindingSource1;
    数据的偶合性太强了,后面处理麻烦
      

  7.   

    kongxiangli(笑看红尘) 
    你认为怎么才能解除耦合?
      

  8.   

    问题终于解决了,TNND,datagridview居然搞出这种问题出来
    只要把datagridview 启用添加去掉,添加启用编辑、启用删除功能(偶不在datagridview编辑数据)。耗了我好多时间