Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在datagridview中的选定行上搜索字符串值?_C#_Datagridview_Row - Fatal编程技术网

C# 如何在datagridview中的选定行上搜索字符串值?

C# 如何在datagridview中的选定行上搜索字符串值?,c#,datagridview,row,C#,Datagridview,Row,我有DataGridView,我已经设法让它工作,这样当用户单击一行时,程序就会用该行的数据填充相应的文本框 如果行包含“exampleValue”,我希望它自动检查radioButton1,否则它应该检查radioButton2 我总是遇到错误“无法将类型'System.Windows.Forms.DataGridViewCellCollection'隐式转换为'bool'” 提前谢谢 编辑:我在这里尝试了这个代码 if (dtgFunc.Rows[e.RowIndex

我有DataGridView,我已经设法让它工作,这样当用户单击一行时,程序就会用该行的数据填充相应的文本框

如果行包含“exampleValue”,我希望它自动检查radioButton1,否则它应该检查radioButton2

我总是遇到错误“无法将类型'System.Windows.Forms.DataGridViewCellCollection'隐式转换为'bool'”

提前谢谢

编辑:我在这里尝试了这个代码

            if (dtgFunc.Rows[e.RowIndex].Cells.Count > 0)
        {
            foreach (DataGridViewCell cell in dtgFunc.Rows[e.RowIndex].Cells)
            {
                if ((string)cell.Value == "seller")
                {
                    rdbSeller.Checked = true;
                    rdbManager.Checked = false;
                }
                else
                {
                    rdbManager.Checked = true;
                    rdbSeller.Checked = false;
                }
            }
        }
但我还是犯了这个错误。想法

Unable to cast object of type 'System.Int32' to type 'System.String'.
您可以使用Linq:

yourDataGridViewCellCollection
   .Cast<DataGridViewCell>()
   .FirstOrDefault(x => x.Value.Equals(exampleValue)) != null
在您当前的代码中:

cell.Value.Equals("seller")

由于cell.Value是一个对象,因此在比较之前,它将检查类型是否兼容

(String)cell.Value
对此

cell.Value.ToString();

它无法将值强制转换为字符串,因此需要使用上面的

请发布代码。@deathismyfriend现在发布了它,但我不知道它有多大帮助,因为我正在用另一种语言编写代码。我只是想知道,如果我想在一行中搜索一个特定的值,然后以某种方式将其转换为布尔值,这样我就可以使“如果行包含'xxx',那么radioButton1.Checked=true”thing您不发布什么示例值,或者您是否使用了它?如果要查找行中包含该值的任何列,则需要搜索该行中的每个单元格,并将值转换为字符串。对单元格进行比较检查,以查看该行是否包含特定字符串。然后你可以检查一下收音机button@deathismyfriend我用我的尝试颠覆了原来的帖子。这就是你的意思吗?
(String)cell.Value
cell.Value.ToString();