index=dataGrid1.CurrentRowIndex;不過注意,當dataGrid排序后 
使用 删除 當前dataGrid1.CurrentRowIndex行 就有出錯

解决方案 »

  1.   

    最好使用绑定,代码如下:
    首先取得数据,放到DataGrid里System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection("server=localhost;database=northWind;uid=sa;password=110");
    conn.Open();
    System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter("select * from student",conn);
    dt = new System.Data.DataSet();
    da.Fill(dt,"student");然后绑定数据集和DataGrid
    DataGrid.SetDataBinding(dt,"student");
    如果需要,可以绑定TextBox来作录入,而用DataGrid显示
    this.textBox16.DataBindings.Add("Text",dt,"student.stuno");
    然后进行数据的操作如:
    增加:
    this.BindingContext[dt,"student"].AddNew();
    删除:
    this.BindingContext[dt,"student"].RemoveAt(this.BindingContext[dt,"student"].Position);
    最后把结果写回数据库:
      

  2.   

    象这样就可避免出错:
    CurrencyManager cm = (CurrencyManager) BindingContext[dataGrid1.DataSource, dataGrid1.DataMember]; 
    用cm.Position控制记录指针。
    新增:cm.AddNew()
    删除:cm.RemoveAt(cm.Position);
      

  3.   

    搞定了刚才的问题,但是又出现了一个新问题,怎么通过列的内容删除行。
    比如有一个CustomerID列,里面有一个数值是ANTON,怎么删除与ANTON有关的行???
      

  4.   

    row=dsOrders1.Customers.FindByCustomerID("ANTON");//CustomerID为主键
    if (row!=null) 
    {
    row.Delete();
    row.EndEdit();
    }