用IsSelect把他们都放到DataRow里,再删

解决方案 »

  1.   

    if(this.dataGrid1.IsSelected(i))
    当你删除第一条记录的时候。这个条件就不成立了。
    如果一定要用你的方法的话。。你自己想想怎么保存选中的记录。
    我也觉得应该要用checkBox来实现。。
      

  2.   

    我按你的意思模拟的做了一个。你看看
    ArrayList aa = new ArrayList();
    int k = 0;
    try
    {
    if (this.sqlConnection1.State.ToString() != "Open")
    this.sqlConnection1.Open();
    if (this.sqlConnection2.State.ToString() != "Open")
    this.sqlConnection2.Open();
    if (ds.Tables["test"].Rows.Count > 0)
    {
    for(int i=0;i<ds.Tables["test"].Rows.Count;i++)
    {
    if(this.dataGrid1.IsSelected(i))
    {

    int c = new int();
    c = i;
    aa.Add(c);
    }
    }
    for(int i=0;i<aa.Count;i++)
    {
    int j =int.Parse(aa[i].ToString());

    // this.BindingContext[this.dataGrid1.DataSource].RemoveAt(i);
    this.sqlDataAdapter1.SelectCommand.CommandText="delete from CD_Custmer where Cust_id="+this.dataGrid1[j,0].ToString();
    this.sqlDataAdapter1.SelectCommand.ExecuteNonQuery();
    string bb="正在删除第"+j.ToString()+"号记录...";
    j = j-k;

    this.ds.Tables["test"].Rows.RemoveAt(j);
    MessageBox.Show(bb);
    k = k+1;
    }

    if (this.sqlConnection1.State.ToString() == "Open")
    this.sqlConnection1.Close();
    if (this.sqlConnection2.State.ToString() == "Open")
    this.sqlConnection2.Close();

    }
    }
    catch(SqlException err)
    {
    MessageBox.Show("Exception: " + err.Message);
    }
    catch(Exception error)
    {
    MessageBox.Show("Exception: " + error.Message);
    }
    你看下,可能还有些小的bug,你看着改下。
    大概的意思就是先删除数据库的,然后再删除DataGrid中的。
    good lucky!