111,126
社区成员
发帖
与我相关
我的任务
分享 private void dgvList_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
if (e.RowIndex >= 0)
{
if (dgvList.Rows[e.RowIndex].Selected == false)
{
dgvList.ClearSelection();
dgvList.Rows[e.RowIndex].Selected = true;
}
if (dgvList.SelectedRows.Count == 1 && e.ColumnIndex != -1)
{
dgvList.CurrentCell = dgvList.Rows[e.RowIndex].Cells[e.ColumnIndex];
}
dgvList.ContextMenuStrip = contextMenuStrip1;
}
else
{
dgvList.ContextMenuStrip = null;
return;
}
}
}
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);
}