写在某Button里面的。
string sql;
StringBuilder buff = new StringBuilder();
oleDbConnection2.Open();
sql = buff.Append("delete from 表1 where name='")
.Append(dataGridView1.SelectedRows[0].Cells[1]).Append("'").ToString();
oleDbInsertCommand1.CommandText = sql;
MessageBox.Show(oleDbInsertCommand1.ExecuteNonQuery().ToString());
oleDbConnection2.Close();
就是想在数据库里删除dgv中被选中的行,运行时出现:
索引超出范围。必须为非负值并小于集合大小。
参数名: index
该怎么办?
另外,应该有比这个更好的方法吧?求教~!
string sql;
StringBuilder buff = new StringBuilder();
oleDbConnection2.Open();
sql = buff.Append("delete from 表1 where name='")
.Append(dataGridView1.SelectedRows[0].Cells[1]).Append("'").ToString();
oleDbInsertCommand1.CommandText = sql;
MessageBox.Show(oleDbInsertCommand1.ExecuteNonQuery().ToString());
oleDbConnection2.Close();
就是想在数据库里删除dgv中被选中的行,运行时出现:
索引超出范围。必须为非负值并小于集合大小。
参数名: index
该怎么办?
另外,应该有比这个更好的方法吧?求教~!
StringBuilder buff = new StringBuilder();
oleDbConnection2.Open();
sql = buff.Append("delete from 表1 where 1=1 ")
if(dataGridView1.SelectedRows.Count>0)
{
buff.Append(" AND name='")
buff.Append(dataGridView1.SelectedRows[0].Cells[1].Value.ToString());
buff.Append("'").ToString();
}
oleDbInsertCommand1.CommandText = sql;
MessageBox.Show(oleDbInsertCommand1.ExecuteNonQuery().ToString());
oleDbConnection2.Close();
你要选择的应该是这一行里面的Cell[1].Controls[0] 的值。这个Controls[0]可能是TextBox,或者是Lable,需要强制转换一下,然后取得他们的Text属性。
最好是用DataKeys获取你选中行的主键值。这样比较方便。
个人意见,呵呵……
我也是新人
要是选中cell的话,可不可以删除cell所在的行?
debug下 改下下标数字看看到底是哪个
建议使用 DataKeys