Wes*_*Wes 3 c# linq datagridview
我是LINQ的新品牌,我正在尝试在我目前的业余爱好项目中使用它.我有一个datagridview每行的第一个单元格是a datagridviewcheckbox,第四个单元格是一个字符串.
如果选中该复选框,我需要将第4个单元格的值添加到列表中.
起初我尝试过:
var selectedID = from c in multiContactLookup.SelectedCells.Cast<DataGridViewCell>()
select multiContactLookup.Rows[c.RowIndex].Cells[4].Value;
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为已检查的单元格是以编程方式取消选择的,因此c绝不是值.
然后我尝试了:
var sel2 = from r in multiContactLookup.Rows.Cast<DataGridViewRow>()
where r.Cells[0].Value is true select r.Cells[4].Value;
Run Code Online (Sandbox Code Playgroud)
但不知怎的,我的语法错了.
使用LINQ,如何选择检查第一个单元格的行,然后选择第一个单元格的值?我是否必须将其拆分为两个集合?
谢谢!
我认为这应该有效:
IEnumerable<string> values = multiContactLookup.Rows.Cast<DataGridViewRow>()
.Where(row => (bool)row.Cells[0].Value)
.Select(row => (string)row.Cells[3].Value);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8702 次 |
| 最近记录: |