C#删除数据库记录不能删除一个,只能删除两个或两个以上 在线等

英雄在线 2009-03-27 09:15:43
if (MessageBox.Show("确定要删除?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}

int chkCount = 0;

for (int i = 0; i < this.dgvGatherInfo.Rows.Count; i++)
{
if (this.dgvGatherInfo.Rows[i].Cells[0].Value != null) //判断该行的复选框是否存在
{
if (this.dgvGatherInfo.Rows[i].Cells[0].Value.ToString() == "True") //判断该复选框是否被选中
{
gaRule.GatherInfoDel(Convert.ToInt32(this.dgvGatherInfo.Rows[i].Cells[1].Value));
}
chkCount++;
}


}
if (chkCount == 0)
{
MessageBox.Show("请选择要删除的行!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
MessageBox.Show("删除成功!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.FormGatherInfo_Load(this,null);


生成通过,运行程序的话在checkbox选择,要选两个或两个以上才能执行删除,选一个的话会提示叫你“选择要删除的行”
调试的时候 只在一个checkbox打勾,程序运行到if (this.dgvGatherInfo.Rows[i].Cells[0].Value != null) //判断该行的复选框是否存在 就会直接跳过到if (chkCount == 0)
不知道怎么回事
大家帮忙看下
...全文
317 34 打赏 收藏 转发到动态 举报
写回复
用AI写文章
34 条回复
切换为时间正序
请发表友善的回复…
发表回复
T3p0 2009-03-27
  • 打赏
  • 举报
回复
不至于搞这么复杂吧,应该是很简单的问题啊,楼主好好调调
英雄在线 2009-03-27
  • 打赏
  • 举报
回复
解决了
我添加了datagridview一个单击cell命令
/// <summary>
/// 单击cell命令
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dgvGatherInfo_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0)
{
this.dgvGatherInfo.Rows[e.RowIndex].Cells[0].Value = this.dgvGatherInfo.Rows[e.RowIndex].Cells[0].EditedFormattedValue.ToString() == "True" ? "False" : "True";
}
}

谢谢大家的帮忙
a6252224 2009-03-27
  • 打赏
  • 举报
回复
string idStr = "";
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox newsCheck = (CheckBox)row.FindControl("CheckBox1");
if (newsCheck != null)
{
if (newsCheck.Checked == true)
{
idStr += GridView1.DataKeys[row.RowIndex].Value.ToString() + ",";
}
}
}
西安风影 2009-03-27
  • 打赏
  • 举报
回复
向这种问题断点调试一下就行了

if (this.dgvGatherInfo.Rows[i].Cells[0].Value.ToString() == "True") //判断该复选框是否被选中
{
gaRule.GatherInfoDel(Convert.ToInt32(this.dgvGatherInfo.Rows[i].Cells[1].Value));
chkCount++;
}
zhoujianzhou 2009-03-27
  • 打赏
  • 举报
回复
gaRule.GatherInfoDel(Convert.ToInt32(dgvGatherInfo.Rows[i].Cells[1].Value));
这句好像有问题吧。。选中的第一个用这句应该不可以删的。。。
Terran5 2009-03-27
  • 打赏
  • 举报
回复
AllowUserToAddRows设置为false,这个可能是主要原因。
如果还不行,试试:
dataGridView1.BeginEdit(true);和
if (dataGridView1.Rows[e.RowIndex].Cells["Column1"].Value.ToString() == "true")
这两个代码加入
xiaolukatie 2009-03-27
  • 打赏
  • 举报
回复
好象以前出现过类似问题
后来做了个投机的办法,就是把第一行的那个有checkbox的那一列选中下,这样就可以删除掉了
dgvGatherInfo.Rows[0].Cells[1].Selected = true;
不知道楼主是不是这种情况
T3p0 2009-03-27
  • 打赏
  • 举报
回复
你的AllowUserToAddRows属性有没有设置为false啊。。。
英雄在线 2009-03-27
  • 打赏
  • 举报
回复
晕 难道是我人品有问题
贫僧又回来了 2009-03-27
  • 打赏
  • 举报
回复
[Quote=引用 24 楼 w530883975 的回复:]
/// <summary>
/// 删除按钮
/// </summary>
/// <param name="sender"> </param>
/// <param name="e"> </param>
private void toolStripButtonDel_Click(object sender, EventArgs e)
{
if (MessageBox.Show("确定要删除?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
re…
[/Quote]
我这选择一个都不会是NULL的啊?
英雄在线 2009-03-27
  • 打赏
  • 举报
回复
/// <summary>
/// 删除按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void toolStripButtonDel_Click(object sender, EventArgs e)
{
if (MessageBox.Show("确定要删除?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}

int chkCount = 0;

for (int i = 0; i < dgvGatherInfo.Rows.Count; i++)
{
if (dgvGatherInfo.Rows[i].Cells[0].Value != null) //判断该行的复选框是否存在
{
if (dgvGatherInfo.Rows[i].Cells[0].Value.ToString() == "True") //判断该复选框是否被选中
{
gaRule.GatherInfoDel(Convert.ToInt32(dgvGatherInfo.Rows[i].Cells[1].Value));
}
chkCount++;
}


}
if (chkCount == 0)
{
MessageBox.Show("请选择要删除的行!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
MessageBox.Show("删除成功!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.FormGatherInfo_Load(this,null);
}
jie3614 2009-03-27
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 hucailin 的回复:]
在执行循环删除的时候设置下从第一行开始
this.dgvGatherInfo.rowType不等于表头
[/Quote]

楼主试试这个方法 我想可以解决你的问题
英雄在线 2009-03-27
  • 打赏
  • 举报
回复
checkbox是在datagridview编辑列上面添加的
上面这段代码是删除按钮事件的代码
贫僧又回来了 2009-03-27
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 w530883975 的回复:]
不是啊
我加载窗体的时候已经设置为false了
[/Quote]
我对你真的很无语了!
问你怎么添加的复选框,具体这段代码写在哪里的?
懒得回复了!
英雄在线 2009-03-27
  • 打赏
  • 举报
回复
不是啊
我加载窗体的时候已经设置为false了
冰凝瞬间1986 2009-03-27
  • 打赏
  • 举报
回复
在执行循环删除的时候设置下从第一行开始
this.dgvGatherInfo.rowType不等于表头
贫僧又回来了 2009-03-27
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 w530883975 的回复:]
对啊
我又设置断点调试了下
说错了
选中N个checkbox 执行删除
只会删除前N-1条记录
[/Quote]
你的datagridview是不是有自动增长列的?
namhyuk 2009-03-27
  • 打赏
  • 举报
回复
为什么不通过BindingSource之类的删?

至少我是不会用这种方式去删数据的。

难道微软的数据库与界面控件的双向数据绑定是白给的吗?INotifyPropertyChanged这东西是摆设吗?

chuxue1342 2009-03-27
  • 打赏
  • 举报
回复
你也可以试试改成这样:

CheckBox cb1 = (CheckBox)(dgvGatherInfo.Rows[i].Cells[0].ConTrols[0]);
if (cb1.Checked)
{
gaRule.GatherInfoDel(Convert.ToInt32(this.dgvGatherInfo.Rows[i].Cells[1].Value))
}

chuxue1342 2009-03-27
  • 打赏
  • 举报
回复
如果是多选框.你最好先找到以后再判断是否选中:
if (this.dgvGatherInfo.Rows[i].Cells[0].Value != null) //判断该行的复选框是否存在
{
if (this.dgvGatherInfo.Rows[i].Cells[0].Value.ToString() == "True") //判断该复选框是否被选中
{
gaRule.GatherInfoDel(Convert.ToInt32(this.dgvGatherInfo.Rows[i].Cells[1].Value));
}
-------------------------------------------------------------------------------------------------------------------
CheckBox cb1 = (CheckBox)(GridView1.Rows[i].Cells[0].FindControl("多选框ID"));
if (cb1.Checked)
{
gaRule.GatherInfoDel(Convert.ToInt32(this.dgvGatherInfo.Rows[i].Cells[1].Value))
}

当然,这句你要判断下:
加载更多回复(14)

111,126

社区成员

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

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

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