Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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# LINQ根据行中的其他单元格选择DataGridView中的特定单元格_C#_Linq_Datagridview - Fatal编程技术网

C# LINQ根据行中的其他单元格选择DataGridView中的特定单元格

C# LINQ根据行中的其他单元格选择DataGridView中的特定单元格,c#,linq,datagridview,C#,Linq,Datagridview,我是LINQ的新成员,我正试图在我目前的爱好项目中使用它。我有一个datagridview,其中每行的第一个单元格是一个datagridviewcheckbox,第四个单元格是一个字符串 如果选中该复选框,我需要将第四个单元格的值添加到列表中 起初我试着: var selectedID = from c in multiContactLookup.SelectedCells.Cast<DataGridViewCell>()

我是LINQ的新成员,我正试图在我目前的爱好项目中使用它。我有一个
datagridview
,其中每行的第一个单元格是一个
datagridviewcheckbox
,第四个单元格是一个字符串

如果选中该复选框,我需要将第四个单元格的值添加到列表中

起初我试着:

var selectedID = from c in multiContactLookup.SelectedCells.Cast<DataGridViewCell>() 
                              select multiContactLookup.Rows[c.RowIndex].Cells[4].Value;
但不知怎么的,我的语法错了

使用LINQ,如何选择选中第一个单元格的行,然后选择第一个单元格的值?我必须把它分成两个系列吗


谢谢大家!

我认为这应该行得通:

IEnumerable<string> values = multiContactLookup.Rows.Cast<DataGridViewRow>()
    .Where(row => (bool)row.Cells[0].Value)
    .Select(row => (string)row.Cells[3].Value);
IEnumerable values=multiContactLookup.Rows.Cast()
.Where(row=>(bool)row.Cells[0].Value)
.Select(row=>(string)row.Cells[3].Value);

也许不是你一直在寻找的答案,但是

DataGridView
(作为大多数win窗体控件)不是从LINQ开始的最佳源代码。大多数集合没有实现正确的
IEnumerable
接口。这就是您需要
Cast()
解决方案的原因

通常,尝试将应用程序的行为从控件转移到数据中。可能您的网格连接到了一个
数据表
。在这种情况下,为更改的数据注册
DataTable
的事件,并使用
DataRow
的值,而不是
DataGridViewRow
及其单元格

IEnumerable<string> values = multiContactLookup.Rows.Cast<DataGridViewRow>()
    .Where(row => (bool)row.Cells[0].Value)
    .Select(row => (string)row.Cells[3].Value);