日期:2014-05-19  浏览次数:20749 次

为什么不能删除DataTable中的当前行?
使用BindingSource,DataTable,绑定到DataGridView,可是无论我在网格中选中哪一行,删除的都是第一行。
删除代码如下:
private   void   btnDetailDel_Click(object   sender,   EventArgs   e)
                {
                        if   (BindingSource.Count   >   0)
                        {
                                语句1:
DataTable.Tables[0].Rows.RemoveAt(BindingSource.Position);
语句2:                
                BindingSource.RemoveCurrent();
语句3:
                                BindingSource.CurrencyManager.RemoveAt(BindingSource.CurrencyManager.Position);
                        }
                }
使用三种语句中任意一种,结果都一样,都是删除第一行,而不是网格中选定的行。
但是BindingSource绑定的普通TextBox显示,却是跟随网格行的变化而变化。

------解决方案--------------------
你首先要得到你选定的行
DataRowView row = this.m_DataSet.Tables[0].DefaultView[this.dataGrid.CurrentRowIndex];
------解决方案--------------------
CurrencyManager gridCurrencyManager =
(CurrencyManager)this.BindingContext[this.dataGridView1.DataSource, this.dataGridView1.DataMember];

gridCurrencyManager.RemoveAt(this.dataGridView1.CurrentRow.Index);