日期:2014-05-18 浏览次数:20904 次
private void button1_Click(object sender, EventArgs e) { DataRowView rowView = this.dataGridView1.CurrentRow.DataBoundItem as DataRowView; if (rowView != null) { DataRow currentRow = rowView.Row; //获取主键 string id = currentRow["ID"]; //调用数据库里的删除方法: string sql = "DELETE FROM TABLE_NAME WHERE ID='" + id + "'"; //数据库操作 } }
------解决方案--------------------
if (dataGridViewX1.Rows.Count > 0) { if (dataGridViewX1.CurrentRow != null) { if (dataGridViewX1.SelectedRows.Count == 0) { dataGridViewX1.Rows.Remove(dataGridViewX1.CurrentRow); dt.AcceptChanges();//绑定datagridview的表 } else { try { for (int i = dataGridViewX1.SelectedRows.Count; i > 0; i--) { dataGridViewX1.Rows.RemoveAt(dataGridViewX1.SelectedRows[i - 1].Index); } dt.AcceptChanges();//绑定datagridview的表 } catch (Exception) { MessageBox.Show("无法删除空行!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } } } else { MessageBox.Show("请选择删除的行!","提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } } else { MessageBox.Show("没有可删除数据!", "提示信息",MessageBoxButtons.OK, MessageBoxIcon.Asterisk); }