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)
不知道怎么回事
大家帮忙看下
{
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)
不知道怎么回事
大家帮忙看下
解决方案 »
- c#下载程序问题
- 弱弱的问:是不是继承类就一定比基类的功能(方法)多
- [[[00983C34]+1C]+24]+260]。。求[]的个数
- C#启动其他程序的问题
- 当下拉矿选择“是”的时候,显示维修费文本矿;当下拉矿选择“否”的时候,不显示维修费文本矿,怎么解决?????????
- 如何在tabpage上的某个控件取得焦点?
- 关于水晶报表的速度和登陆
- 求助:使用System.Windows.Forms.Timer类遇到的奇怪问题
- System.Diagnostics.Process.Start("IEXPLORE.EXE","http://expert.csdn.net")抛异常:找不到指定的文件
- 怎样把string转换成int阿?
- C# tcp 聊天室程序添加文件发送功能
- datagridview分页显示记录问题
试试
{
if (this.dgvGatherInfo.Rows[i].Cells[0].Value.ToString() == "True") //判断该复选框是否被选中
{
gaRule.GatherInfoDel(Convert.ToInt32(this.dgvGatherInfo.Rows[i].Cells[1].Value));
}
chkCount++;
}
在if前加个判断,判断当前行的类型是否为数据行,如果为表头的话。肯定不能算。。
可以这样来判断吗?我一般是用if (Convert.ToBoolean(dgr.Cells["是否选择"].Value) == false)
if(找到的CheckBox.checked==true){...}
放到监视里就可以看到的,我觉也是表头的问题
{
bool isChecked = ((CheckBox)GridView1.Rows[i].FindControl("CheckBox1")).Checked;
if (isChecked)//找到模板中的CheckBox
{
numOfChecked += 1;//计数
}
}
if (numOfChecked == 1)
{
for (int i = 0; i < this.GridView1.Rows.Count; i++)
{
bool isChecked = ((CheckBox)GridView1.Rows[i].FindControl ("CheckBox1")).Checked;
if (isChecked)
{
//有选中。
} } }
else
{
Response.Write(" <Script language=JavaScript>alert('您只能选择一条记录!');
</Script>");
}
你用的DataGridView?
调试看到的this.dgvGatherInfo.Rows[i].Cells[0].Value 的值都是null
不知道怎么回事那个表头是什么意思啊
private void button1_Click_1(object sender, EventArgs e)
{
int chkCount = 0; for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
{
if (this.dataGridView1.Rows[i].Cells[0].Value != null) //判断该行的复选框是否存在
{
if (this.dataGridView1.Rows[i].Cells[0].Value.ToString() == "True") //判断该复选框是否被选中
{
MessageBox.Show("choose");
}
chkCount++;
}
if (chkCount == 0)
{
MessageBox.Show("fail");
}
}
我又设置断点调试了下
说错了
选中N个checkbox 执行删除
只会删除前N-1条记录
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))
}当然,这句你要判断下:
if (cb1.Checked)
{
gaRule.GatherInfoDel(Convert.ToInt32(this.dgvGatherInfo.Rows[i].Cells[1].Value))
}
this.dgvGatherInfo.rowType不等于表头
我加载窗体的时候已经设置为false了
上面这段代码是删除按钮事件的代码
/// 删除按钮
/// </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);
}
后来做了个投机的办法,就是把第一行的那个有checkbox的那一列选中下,这样就可以删除掉了
dgvGatherInfo.Rows[0].Cells[1].Selected = true;
不知道楼主是不是这种情况
如果还不行,试试:
dataGridView1.BeginEdit(true);和
if (dataGridView1.Rows[e.RowIndex].Cells["Column1"].Value.ToString() == "true")
这两个代码加入
这句好像有问题吧。。选中的第一个用这句应该不可以删的
{
gaRule.GatherInfoDel(Convert.ToInt32(this.dgvGatherInfo.Rows[i].Cells[1].Value));
chkCount++;
}
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() + ",";
}
}
}
我添加了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";
}
}谢谢大家的帮忙