C# 即使满足条件,也跳过C if语句

C# 即使满足条件,也跳过C if语句,c#,if-statement,C#,If Statement,我正在为学校写一个c语言的basic程序,我遇到了一个if语句的问题,其中条件得到满足,但代码被跳过,就好像条件没有得到满足一样 //this runs when i select a cell on the dataGridView private void dataGridView1_CellClick(object sender,DataGridViewCellEventArgs e) { string estado = ""; if (e.RowIndex &g

我正在为学校写一个c语言的basic程序,我遇到了一个if语句的问题,其中条件得到满足,但代码被跳过,就好像条件没有得到满足一样

//this runs when i select a cell on the dataGridView
private void dataGridView1_CellClick(object sender,DataGridViewCellEventArgs e)
{    

    string estado = "";
    if (e.RowIndex >= 0)
    {
        DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
        id_lbl.Text = row.Cells[0].Value.ToString();
        nombre_lbl.Text = row.Cells[1].Value.ToString();
        apellido_lbl.Text = row.Cells[2].Value.ToString();
        estado = row.Cells[7].Value.ToString();
    }

    id_lbl.Visible = true;
    nombre_lbl.Visible = true;
    apellido_lbl.Visible = true;

    if(estado == "Activo")
    {
        baja_btn.Enabled = true;
    }
    else if (estado == "NoActivo")
    {
        alta_btn.Enabled = true;
    }
    else
    {
        MessageBox.Show(estado);
    }
}
代码运行,但若语句直接跳转到else代码,消息框显示Activo,无论如何,baja_btn.Enabled=true;不跑。 如果我选择带有NOACTIO的行,情况也是如此。如果直接跳到别的

注意:在输入if语句之前,estado的实际值是Activo。因此,它应该进入第一个条件,但它会一直跳到其他条件。

两个字符串不相等。若要查看位置和原因,请在以下情况之前尝试调试报告:


我忘了提到我也尝试了ifestado.EqualsActivo,但得到了相同的结果..直接跳到了else代码。你确定字符串真的相等吗?您是否检查了estado字符串开头和结尾的空格?另外,您是否验证了大小写是否相同?您必须提供来自row.Cells[7].Value.ToString的estado字符串结果。可能单元格值中有一个空格,请在代码行处放置一个断点:id\u lbl.Visible=true;检查estadoTry this的值:String.equalStato.Trim、Activo、StringComparison.OrdinalIgnoreCase它将删除空格并比较忽略的案例谢谢。。另外,我在estado上的Activo之后有两个空格,但这不在sql注册表上。。这很奇怪,但至少现在我知道我的问题以及从哪里开始寻找解决方案。。非常感谢已测试[Activo]0041 0063 0074 0069 0076 006f 0020 0020 0020长度10*实际[Activo]0041 0063 0074 0069 0076 006f长度6@JoelContreras:空格可以是不同的“”、不间断空格、窄空格等。。因为我的String.equalstato.Trim、Activo、StringComparison.ordinallingorecase没有帮助,所以您不仅有两个序数空格,还有一些其他的。编码的测试值是多少?测试的[Activo]0041 0063 0074 0069 0076 006f 0020 0020 0020 0020长度10实际[Activo]0041 0063 0074 0069 0076 006f长度6@JoelContreras:您有四个空格。因此,如果String.equalstato.Trim、Activo、StringComparison.ordinallingorecase{baja_btn.Enabled=true;}应该可以工作,非常感谢。。很明显,我第一次检查你的线路时,我少了一组额外的somewere或somewere。。如果String.equalsetado.Trim、Activo、StringComparison.ordinallingorecase{baja_btn.Enabled=true;}像一个符咒一样工作,则直接复制并粘贴。。将此标记为答案。。再次,非常感谢。。
  ... 

  String report = String.Format(
    "Tested [{0}] encoded {1} of length {2}\r\nActual [{3}] encoded {4} of length {5}",
    estado,
    String.Join(" ", estado.Select(c => ((int) c).ToString("x4"))),
    estado.Length, 
    "Activo", // <- copy/paste all "Activo" from the if
    String.Join(" ", "Activo".Select(c => ((int) c).ToString("x4"))),
    "Activo".Length);

  MessageBox.Show(report);

  if(estado == "Activo") // <- if of the question
    ...
 if (estado.TrimEnd() == "Activo") 
 {
    baja_btn.Enabled = true;
 }