关于DataGrid中如何添加,删除一行? 在DATAGRID里填加一行,只要将记录指向最后,就可以编辑了。想要删除,只要选择这行并按‘DELETE’键就行,不过,这只是在缓存里的增减,想要真正提交到数据库还要执行数据集(表)的AcceptChanges();和数据适配器的UPDATE()着两个方法。这些内容帮助里有。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 准确地讲,应该是往绑定的数据源添加或删除行。试试这个:\\添加dg.BindingContext[dg.DataSource,dg.DataMember].AddNew();\\删除int index;dg.BindingContext[dg.DataSource,dg.DataMember].RemoveAt(index); 你应该是用数据绑定做的,可以这样:int aaa;//aaa是随便的一个名字aaa = dataGrid1.CurrentRowIndex;dataSet11.Tables[0].Rows[aaa].Delete();这样数据集中的数据将被更改,用sqldataadapter.update(你的数据集名);可以把数据集的数据填充到数据库中;增加~你自己来吧! dy_2000_abc(芝麻开门)兄猛呀!^_^ dy_2000_abc(芝麻开门)真是好厉害 谢谢各位,我不是不知道如何更新数据源的问题,而是不知道通过那些触发的事件来写这些代码。今天终于找到了,通过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(); } trackBar 控件的使用 求几个正则表达式和datagridview code ??C#如何删除excel得前五行 C#的画笔对象能否画复合颜色渐近的线? 静态方法是不是线程安全的?Socket类中的Receive和Send方法是不是线程安全的? exchange开发问题一则 求高手指教!!! 怎么将收藏夹里面的网页地址批量放到txt winform程序调试中出现异常 问一个 弱弱的问题 如何去掉 网页 图片左上角的 红色小叉叉 微软宣扬Visual Studio.Net的新版本 从库中读取图片一问
试试这个:
\\添加
dg.BindingContext[dg.DataSource,dg.DataMember].AddNew();
\\删除
int index;
dg.BindingContext[dg.DataSource,dg.DataMember].RemoveAt(index);
int aaa;//aaa是随便的一个名字
aaa = dataGrid1.CurrentRowIndex;
dataSet11.Tables[0].Rows[aaa].Delete();
这样数据集中的数据将被更改,用
sqldataadapter.update(你的数据集名);
可以把数据集的数据填充到数据库中;
增加~你自己来吧!
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();
}