在DATAGRID里填加一行,只要将记录指向最后,就可以编辑了。想要删除,只要选择这行并按‘DELETE’键就行,不过,这只是在缓存里的增减,想要真正提交到数据库还要执行数据集(表)的
AcceptChanges();
和数据适配器的
UPDATE()
着两个方法。这些内容帮助里有。

解决方案 »

  1.   

    准确地讲,应该是往绑定的数据源添加或删除行。
    试试这个:
    \\添加
    dg.BindingContext[dg.DataSource,dg.DataMember].AddNew();
    \\删除
    int index;
    dg.BindingContext[dg.DataSource,dg.DataMember].RemoveAt(index);
      

  2.   

    你应该是用数据绑定做的,可以这样:
    int aaa;//aaa是随便的一个名字
    aaa = dataGrid1.CurrentRowIndex;
    dataSet11.Tables[0].Rows[aaa].Delete();
    这样数据集中的数据将被更改,用
    sqldataadapter.update(你的数据集名);
    可以把数据集的数据填充到数据库中;
    增加~你自己来吧!
      

  3.   

    dy_2000_abc(芝麻开门)兄猛呀!^_^
      

  4.   

    dy_2000_abc(芝麻开门)真是好厉害
      

  5.   

    谢谢各位,我不是不知道如何更新数据源的问题,而是不知道通过那些触发的事件来写这些代码。今天终于找到了,通过DataTable.RowDeleted与RowChanged事件.还有DataGrid的CurrentCellChanged事件,在此还是谢谢各位的参与!我的代码如下:
    private void dgrDepCheckOut_CurrentCellChanged(object sender, System.EventArgs e)
    {
    int i_CurRowIndex;
    int i_ORowsCount;//原行数
    int i_NRowsCount;//现行数
    i_CurRowIndex=dgrDepCheckOut.CurrentRowIndex;
    i_ORowsCount=((DataTable)(dgrDepCheckOut.DataSource)).Rows.Count;
    i_NRowsCount=dgrDepCheckOut.VisibleRowCount;
    //如果现行数比原行数大1,而且当前行等于现行数-1,则增加新行;
    if(((i_NRowsCount-i_ORowsCount)==1) && (i_CurRowIndex==i_ORowsCount))
    {
    this.AddNewRow(); }

    }
    private void AddNewRow()
    {
    DataRow m_Row;
    m_Row=this.m_dtsdgrSource.NewRow();
    m_Row["OPTypeName"]=GetOPTypeName(OPType.InCash);
    m_Row["OPCashValue"]=0;
    m_Row["PaymentMode"]=strDefault;
    m_Row["TargetAccountID"]=strDefault;
    this.m_dtsdgrSource.Rows.Add(m_Row);
    this.m_dtsdgrSource.AcceptChanges();
    }
    private void dgrDepCheckOut_RowDeleted(object sender,DataRowChangeEventArgs e)
    {
    this.m_dtsdgrSource.Rows.Remove(e.Row);
    this.m_dtsdgrSource.AcceptChanges();
    }