C#中从DataGridCiew中删除被选择的项

amoy_yang 2010-11-21 07:26:51
在DataGridView 中添加一个 ConTextMenuStrip 控件 并为DataGridView指定 现在ConTextMwnuStrip中添加一个MenuStrip“删除” 在DataGridView中选择一条记录点击右键选择 删除 !!!
求 代码
...全文
260 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
a466247623 2010-12-25
  • 打赏
  • 举报
回复
string strId = dataGridView1.CurrentRow.Cells[0].Value.ToString();
String strSql = String.Format("delete from table where 主键= '{0}'", strId);
if (MessageBox.Show("确定要删除此行吗?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
//执行语句 }
shighui 2010-12-24
  • 打赏
  • 举报
回复
kjb ,ip wh i wf ka ,wq wu wf kcg ?
a466247623 2010-12-24
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 luojie81 的回复:]
首先说明,不管是不是整行选择都是可以删除的;
确认一下是否进行数据库操作?
需要操作数据库的话用以下代码:

C# code

string strId = dataGridView1["Id", dataGridView1.CurrentCell.RowIndex].Value.ToString();
String strSql = String.Forma……
[/Quote]
globe.SqlConn这个需要引用什么?写上去我的直接报错
luojie81 2010-11-23
  • 打赏
  • 举报
回复
多不起,没有前面回复没有考虑多行选择.
如果有删除多行的话,就循环一下
需要更新数据库:

string strId = string.Empty;
SqlCommand cmd = new SqlCommand(string.Empty, globe.SqlConn);
SqlTransaction myTran = globe.SqlConn.BeginTransaction();
cmd.Transaction = myTran;
try
{
for (int i = 0; i < dataGridView1.SelectedRows.Count; i++)
{
strId = dataGridView1["Id", dataGridView1.CurrentCell.RowIndex].Value.ToString();
cmd.CommandText = String.Format("delete from table where Id = '{0}'", strId);
cmd.ExecuteNonQuery();
}
myTran.Commit();
}
catch (Exception err)
{
if (myTran != null)
{
myTran.Rollback();
}
MessageBox.Show("删除失败! " + err.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}

不需要更新数据库:

for (int i = 0; i < dataGridView1.SelectedRows.Count; i++)
{
dataGridView1.SelectedRows[i].Index;
dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
}
luojie81 2010-11-23
  • 打赏
  • 举报
回复
首先说明,不管是不是整行选择都是可以删除的;
确认一下是否进行数据库操作?
需要操作数据库的话用以下代码:

string strId = dataGridView1["Id", dataGridView1.CurrentCell.RowIndex].Value.ToString();
String strSql = String.Format("delete from table where Id = '{0}'",strId);
SqlCommand cmd = new SqlCommand(strSql, globe.SqlConn);
cmd.ExecuteNonQuery();

不需要操作数据库用以下代码:

dataGridView1.Rows.RemoveAt(dataGridView1.CurrentCell.RowIndex);

jointan 2010-11-22
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 hch126163 的回复:]
如果DataGridView 设置了整行选择,可以用2楼的方法
private void contextMenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
foreach (DataGridViewRow item in this.dataGridView.SelectedRows)
……
[/Quote]

我不太确定这段方法能不能行的通,因为不知
this.dataGridView.Rows.Remove(item);
是否会改变SelectedRows的大小,如果会改变,那么,这个foreach会出错.


SendKeys.Send("{DEL}");
是个绝对正确的方法,而且,DataGridView由于是.Net自带的控件,对于键鼠的操作的响应,肯定是基于更底层的方式实现的,效率肯定比Remove高.

zhangzhen_927116 2010-11-22
  • 打赏
  • 举报
回复
都是高手挖
Ryan20082009 2010-11-22
  • 打赏
  • 举报
回复
protected void Button7_Click(object sender, EventArgs e)
{
for (int i = 0; i < this.GridView1.Rows.Count; i++)
{
CheckBox cb = (CheckBox)this.GridView1.Rows[i].FindControl("chkCheck");

if (cb.Checked)
{
string strCid = this.GridView1.DataKeys[i].Value.ToString();
if (BLL.costBll.dCost(strCid))
{
Response.Write("<script>alert('删除" + strCid + "成功!')</script>");
}
else
{
Response.Write("<script>alert('请选择!')</script>");
}
}

}
this.bind();
}
看下应该可以。。。
beautiful_melody 2010-11-22
  • 打赏
  • 举报
回复
SendKeys.Send("{DEL}");
amoy_yang 2010-11-22
  • 打赏
  • 举报
回复
楼上的 我是用 ConTextMenuStrip 控件 考试要求
sweet_dogltx 2010-11-22
  • 打赏
  • 举报
回复
不用这么麻烦
直接用一个删除按钮

private void btnDeleteView_Click(object sender, EventArgs e)
{
if (dgv.SelectedRows.Count == 0)
{
MessageBox.Show("请在数据列表中选择一行数据!","注意!");
return;
}
DataRowView drv = dgv.SelectedRows[0].DataBoundItem as DataRowView;
drv.Delete();//表面上的删除 实际上没有删除
}

如果想在数据库中删除 还是要获得这条记录的主键 然后用sql删除
amoy_yang 2010-11-22
  • 打赏
  • 举报
回复
如果 不是选择整行了
amoy_yang 2010-11-22
  • 打赏
  • 举报
回复
谢谢大家 新手 还需各位前辈多多指教
perisonchen 2010-11-21
  • 打赏
  • 举报
回复
SendKeys.Send("{DEL}");
hch126163 2010-11-21
  • 打赏
  • 举报
回复
如果DataGridView 设置了整行选择,可以用2楼的方法
private void contextMenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
foreach (DataGridViewRow item in this.dataGridView.SelectedRows)
{
this.dataGridView.Rows.Remove(item);
}
}

否则使用1楼的方法
jointan 2010-11-21
  • 打赏
  • 举报
回复
SendKeys.Send("{DEL}");
guyoujing 2010-11-21
  • 打赏
  • 举报
回复
新手学习。。
colorall 2010-11-21
  • 打赏
  • 举报
回复
private void contextMenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
foreach (DataGridViewRow item in this.dataGridView.SelectedRows)
{
this.dataGridView.Rows.Remove(item);
}
}
hookyzlr 2010-11-21
  • 打赏
  • 举报
回复
为datagridview增加这个事件:
private int id;
private void dataGridView1_CellContextMenuStripNeeded(object sender, DataGridViewCellContextMenuStripNeededEventArgs e)
{
id = dataGridView1.Rows[e.RowIndex].Cells[你的ID所在列].Value;
}
右键菜单单击事件:
private void contextMenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
删除。
}

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧