List<MyItem> items = new List<MyItem>();
foreach (DataGridViewRow dr in dataGridView1.Rows)
{
MyItem item = new MyItem();
foreach (DataGridViewCell dc in dr.Cells)
{
...build out MyItem....based on DataGridViewCell.OwningColumn and DataGridViewCell.Value
}
items.Add(item);
}
var Result = dataGridView1.Rows.OfType<DataGridViewRow>().Select(
r => r.Cells.OfType<DataGridViewCell>().Select(c => c.Value).ToArray()).ToList();
或获取值的字符串字典
var Result = dataGridView1.Rows.OfType<DataGridViewRow>().Select(
r => r.Cells.OfType<DataGridViewCell>().ToDictionary(c => dataGridView1.Columns[c.OwningColumn].HeaderText, c => (c.Value ?? "").ToString()
).ToList();
var list = (from row in dataGridView1.Rows.Cast<DataGridViewRow>()
from cell in row.Cells.Cast<DataGridViewCell>()
select new
{
//project into your new class from the row and cell vars.
}).ToList();
6条答案
按热度按时间zzzyeukh1#
3b6akqbq2#
如果您使用DataSource系结清单,则可以使用下列方式转换回:
v7pvogib3#
或获取值的字符串字典
hpxqektj4#
或者一条直线
xxb16uws5#
IEnumerable.OfType<TResult>
扩展方法是您最好的朋友。下面是我如何通过LINQ查询来实现它:dced5bon6#
VB语言:
C#: