dataTable 绑定 DataGridView   后  用 dataRow.Delete() 方法删除 dataTable 表中数据 后
该行数据在 DataGridView    中为不可见。
但是 现在假设:
dataTable 有 5 行数据 在DataGridView 中 顺序显示 为
0,
1,
2,
3,
4
当 删除了额 数据行 2,后 
在DataGridView 中 顺序显示 为
0,
1,
3,
4
此时 DataGridView 当前行 为 数据行 3,但 问题是 此时再调用dataRow.Delete()  删除时 不起作用!应该 此时dataTable   游标 还是指向 dataTable 的数据行2(数据行2 只是被做了删除标记 并没有被删除)在这种情况下,请问 如何移动 dataTable    的游标 让她 跟 DataGridView  显示时 的当前行 同步谢谢! 困惑好久了

解决方案 »

  1.   

    指定删除第几行不行吗?
    dataTable.Rows.RemoveAt(行号);
      

  2.   

    删除一行记录后调用dataTable.AcceptChanges()方法
      

  3.   

    不是很清楚,楼主在代码是怎么删除的.可直接删除dataGridView上的行,没出现楼主说的"游标"不移动问题
    dataGridView1.Rows.Remove(dataGridView1.CurrentRow);
      

  4.   

     xie谢谢 小强  但是 这样出现了 一个 问题  如果我用你的方法在删除最后一行数据时 
            private void dgView_CrtPeriod_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
            {
                (sender as DataGridView).Rows[e.RowIndex].ErrorText = null;            DateTime startDate = (DateTime)(sender as DataGridView).Rows[e.RowIndex].Cells[this.ColStartDate.Index].Value; //// 此处 出现错误  错误提示 e.RowIndex threw  an exception 
    // of type "System.NullReferenceException"   请问为什么 ?
      

  5.   

    那是因为最好一行为奴奴null,所以引发错误,真正使用时,当然要加一个判断:if (dataGridView1.CurrentRow != null)
                {
                    dataGridView1.Rows.Remove(dataGridView1.CurrentRow);
                }
      

  6.   

    我对 每一行数据都进行数据验证的
    在删除最后一行数据时     DataGridView  也对 数据进行行 验证 所以就出错了 
                    private   void   dgView_CrtPeriod_RowValidating(object   sender,   DataGridViewCellCancelEventArgs   e) 
                    { 
                            (sender   as   DataGridView).Rows[e.RowIndex].ErrorText   =   null;                         DateTime   startDate   =   (DateTime)(sender   as   DataGridView).Rows[e.RowIndex].Cells[this.ColStartDate.Index].Value;   ////   此处   出现错误     错误提示   e.RowIndex   threw     an   exception   
    //   of   type   "System.NullReferenceException"       请问为什么   ?
      

  7.   

    DataTable.DefaultView[DataGrid.CurrentRowIndex].Row.Delete();
      

  8.   

    我们在操作对象的时候,要确保该对象不为null,因为你在RowValidating里也调用了该对象(当前行),所以此事件里也要判断该对象是否为null
      

  9.   

    恩 谢谢你啊  小强, 我发现虽然产生了 异常但是不影响数据 操作 所以 我就捕获了该异常 只是没有进行处理。但是好像 异常产生的不是当前行 ,我现在还没有搞明白!  因为 我在 验证 数据行时 也 判断了当前行 是否为null