111,126
社区成员
发帖
与我相关
我的任务
分享
int stVoucherID = Convert.ToInt32(this.dataGridView1.CurrentRow.Cells["VoucherID"].Value);
DataTable dt = (DataTable)this.dataGridView1.DataSource;
for (i = 0; i < dt.Rows.Count; i++)
{
if (Convert.ToInt32(dt.Rows[i]["VoucherID"])==stVoucherID)//错误指到这里
{ dt.Rows[i].Delete(); }
}
this.dataGridView1.Refresh();
DataTable dt = (DataTable)this.dataGridView1.DataSource;
for (i = 0; i < dt.Rows.Count; i++)
{
if (Convert.ToInt32(dt.Rows[i]["VoucherID"])==stVoucherID)//错误指到这里
{ dt.Rows[i].Delete(); }
}
不要用DataTable直接
for (i = 0; i < this.dataGridView1.Rows.Count; i++)
{
if (Convert.ToInt32(this.dataGridView1t.Rows[i]["VoucherID"])==stVoucherID)//错误指到这里
{
this.dataGridView1t.Rows.RemoveAt(this.dataGridView1.Rows[i].Index);
}
}
for (i = 0; i < dt.Rows.Count; i++)
{
if (Convert.ToInt32(dt.Rows[i]["VoucherID"])==stVoucherID)
{
dt.Rows.RemoveAt(this.dataGridView1.Rows[i].Index);
//dt.Rows[i].Delete();
}
}
dt.Rows[i].Delete(); dt.Rows.Remove(yourRow);//yourRow:要删除的行dt.Rows.RemoveAt(index);//index:要删除行的索引