删除所选行

时间:2014-03-30 19:04:32

标签: c# database dataset

我想从我的数据集中删除一行,更新列表框的内容并更新数据库的内容。我有以下代码,但似乎没有任何反应,所有行仍然存在。 我不做什么?

 DataSet ds = new DataSet();

    SqlDataAdapter daf = new SqlDataAdapter();
    ds.Tables["Film"].Constraints.Add("PK_Film", ds.Tables["Film"].Columns["id"], true);

    int ind = listBoxchildren.SelectedIndex;
    listBoxchildren.DataSource = null;

    ds.Tables["Film"].Rows[ind].Delete();
        ds.Tables["Film"].AcceptChanges();

    daf.Update(ds,"Film");
    listBoxchildren.DataSource = ds;
    listBoxchildren.DisplayMember = "Director.fk_FilmDir.title";

1 个答案:

答案 0 :(得分:0)

在删除项目之前取消绑定数据源,

int selectedIndex = listBoxchildren.SelectedIndex;
listBoxchildren.DataSource = null;

ds.Tables["Film"].Rows[selectedIndex].Delete();
ds.Tables["Film"].AcceptChanges();
daf.Update(ds,"Film");
listBoxchildren.DataSource = ds;