在 C# 中使用绑定源在数据网格视图中计算填充行问题



我正在使用Json使用BindingSource方法填充DataGridView。当我继续前进时,我想知道为什么行数仍然是 1,而根据我的 where 语句没有选择任何值。请帮助我。

这是我的示例代码:

public void JsonPopulateDGV(string JsonDir, int partsId, string fileName)
{
string json = File.ReadAllText(JsonDir);
var jSectionCollection = JsonConvert.DeserializeObject<JSectionCollection>(json) ?? new JSectionCollection();
BindingSource src = new BindingSource();
src.DataSource = jSectionCollection.JSections.Where(x => x.PartsId == partsId).Where(s=>s.FileDir == fileName);
dataGridSections.DataSource = src;
Console.WriteLine(src.Count);
}

您需要通过调用ToArrayToList来执行Where。您还可以将两个 where 语句合并为一个:

src.DataSource = jSectionCollection.JSections
.Where(x => x.PartsId == partsId && x.FileDir == fileName)
.ToList();

最新更新