2010-02-23 15:05
DataView dv = ((DataTable)dataGrid.DataSource).DefaultView;
ArrayList rows = new ArrayList();
//这里非常重要,按逆序保存被选中的行,不要马上删,一删其它选中状态就没了
for(int i=dv.Count - 1; i>=0; i--)
{
if (dataGrid.IsSelected(i))
{
rows.Add(dv[i]);
}
}
//倒过来,从尾部开始把选中的行都删了
foreach(object row in rows)
{
DataRowView dr = (DataRowView)row;
dr.Delete();
}
DataGrid选定多行
DataView dv = ((DataTable)dataGrid.DataSource).DefaultView;
ArrayList rows = new ArrayList();
for(int i=dv.Count - 1; i>=0; i--)
{
if (dataGrid.IsSelected(i))
{
rows.Add(dv[i]);
}
}
foreach(object row in rows)
{
DataRowView dr = (DataRowView)row;
dr.Delete();
}
DataTable dt = (DataTabledataGrid1.DataSource;
for (int i=dt.Rows.Count -1;i>=0;i--)
{
if (dataGrid1.IsSelected(i))
{
string SqlQuery ="delete from info where id='"+dt.Rows[i]["ID"].ToString ()+"'";
SqlConnection conn = new SqlConnection ("server=newer;database=northwind;uid=sa;password=sa");
conn.Open();
SqlCommand cmd = new SqlCommand (SqlQuery,conn);
cmd.ExecuteNonQuery ();
conn.Close ();
}
}