日期:2014-05-17  浏览次数:20922 次

C# if语句为什么只执行else的,我想根据选中的datagridview的行中的某项值(为Bool型)进行判断
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dataGridView1.SelectedRows[0].Cells[9].Value.ToString() == "0")
            {
                FayaoquerenForm frm2 = new FayaoquerenForm(dataGridView1.SelectedRows[0].Cells[0].Value.ToString(), dataGridView1.SelectedRows[0].Cells[2].Value.ToString(), dataGridView1.SelectedRows[0].Cells[3].Value.ToString(), dataGridView1.SelectedRows[0].Cells[7].Value.ToString());
                //所带参数是datagridview选择行的指定列里面的值 
                frm2.ShowDialog();
            }
            else
            {
                MessageBox.Show("此处方药品已取!");
            }
        }

------最佳解决方案--------------------
datagridview中的Bool型要这样判断
把这句
  if (this.dataGridView1.SelectedRows[0].Cells[9].Value.ToString() == "0")
改成
  if (this.dataGridView1.SelectedRows[0].Cells[9].EditedFormattedValue.ToString().Trim() == "True"
这样
------其他解决方案--------------------
请 ... Debug ...
------其他解决方案--------------------
断点调试this.dataGridView1.SelectedRows[0].Cells[9].Value.ToString()看是什么
------其他解决方案--------------------
不知道你的bool型是什么情况下的,是根据sql语句来的,还是根据复选框的选中状态来的,
if (this.dataGridView1.SelectedRows[0].Cells[9].EditedFormattedValue.ToString().Trim() == "True"

这个只对datagridview中第9列为复选框的时候,在有用
------其他解决方案--------------------
引用:
断点调试this.dataGridView1.SelectedRows[0].Cells[9].Value.ToString()看是什么


------其他解决方案--------------------
引用:
不知道你的bool型是什么情况下的,是根据sql语句来的,还是根据复选框的选中状态来的,
if (this.dataGridView1.SelectedRows[0].Cells[9].EditedFormattedValue.ToString().Trim() == "True"

这个只对datagridview中第9列为复选框的时候,在有用
可以了谢谢
------其他解决方案--------------------
bool result=this.dataGridView1.SelectedRows[0].Cells[9].EditedFormattedValue.ToString().Trim();
if(result);;
else //
------其他解决方案--------------------
不错!== "True"