此代码执行但不执行操作? [英] this code getting executed but not doing the action?

查看:101
本文介绍了此代码执行但不执行操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是用datagridview中的复选框删除数据行







private void btndelete_Click(对象发件人,EventArgs e)

{



试试

{

foreach (dataGridView1.Rows中的DataGridViewRow行)

{

object cell = row.Cells [dgvdel]。值;

if(cell ==是)

{



if(MessageBox.Show(想要删除,不要,MessageBoxButtons .YesNo)== DialogResult.OK);

{

javidDataSet.sample_data.Rows [row.Index] .Delete();

sample_dataTableAdapter.Update(javidDataSet);

}



}

}

}



catch(例外情况)

{

}





}



它没有进入消息框循环...

this is to delete the data row with checkbox in datagridview



private void btndelete_Click(object sender, EventArgs e)
{

try
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
object cell = row.Cells["dgvdel"].Value;
if (cell =="yes")
{

if (MessageBox.Show("Want to delete","Don't", MessageBoxButtons.YesNo) == DialogResult.OK) ;
{
javidDataSet.sample_data.Rows[row.Index].Delete();
sample_dataTableAdapter.Update(javidDataSet);
}

}
}
}

catch (Exception ex)
{
}


}

its not entering into the messagebox loop...

推荐答案

我们无法为您解决此问题 - 我们无法单独运行该代码并获得与您的数据相同的结果。



所以从调试器开始。

在这一行放一个断点:

We can't solve this for you - we can't run that code in isolation and get teh same results you do with your data.

So start with the debugger.
Put a breakpoint on this line:
if (cell =="yes")

并运行你的应用。当它到达断点时,它会停止,让你看看究竟发生了什么。

看看cell的内容 - 它是一个字符串吗?它在确切的文本中是否包含是,或者是字符串是,还是是?



我怀疑因为单元格是一个对象,它是在从单元格返回的值和字符串yes之间进行参考比较 - 在​​这种情况下请尝试这样做:

And run your app. when it hits the breakpoint, it will stop, and let you look at exactly what is going on.
Look at the content of "cell" - is it a string? Does it contain "yes" in that exact text, or is the string "Yes", or "YES"?

I suspect that because cell is an object, it's doing a reference comparison between the value returned from your cell and the string "yes" - in which case try this instead:

foreach (DataGridViewRow row in dataGridView1.Rows)
    {
    string cell = (string) row.Cells["dgvdel"].Value;
    if (cell.ToLower() == "yes")
        {



编译时几乎肯定会收到警告:


You are almost certainly getting a warning for this when you compile:

Possible unintended reference comparison

如果是这样,那么您应该始终将警告视为错误以避免此类问题。

If so, then you should always treat warnings as errors to avoid this kind of problem.


这篇关于此代码执行但不执行操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆