将datarow值与if中的字符串进行比较

问题描述:

我有一个应用程序,它将用户选择的值存储到我的数据集中填充的数据表中的值.我需要基于此比较在表中设置另一列.但是这种比较是行不通的.它始终返回false,而不输入if条件.

I have an application which stores a user selected value to the value in my dataset filled datatable. I need to set another column in the table based on this comparison. But the comparison is not working. It always returns false, not entering in the if condition.

foreach (DataRow dr in dsQuestions.Tables[0].Rows)
{
    if (dr["Data"] == indicater[0])
    {
        dr["IsSelected"] = true;
    }
}

indiactor [0] 是一个字符串数组,而 dr ["data"] 也是string类型,但显示警告,提示您需要使用字符串类型.

indiactor[0] is a string array and dr["data"] is also of type string but it shows a warning that it needs to a string type.

首先不能使用==比较字符串,而应使用equals方法:

First of all string can't compare using == you should use equals method:

foreach (DataRow dr in dsQuestions.Tables[0].Rows)
            {
               if (dr["Data"].tostring().Equals(indicater[0]))
                {
                    dr["IsSelected"] = true;
               }