简体   繁体   English

使用C#中的绑定源计算datagridview中的填充行问题

[英]Counting filled rows issue in datagridview using binding source in C#

I'm using Json to populate DataGridView with BindingSource method. 我正在使用Json用BindingSource方法填充DataGridView。 As I go along I wonder why does the count of rows are still 1, when there is no value being selected according to my where statement. 随着我的前进,我想知道为什么当根据我的where语句没有选择任何值时,行数为什么仍然为1。 Please help me. 请帮我。

Here is my sample code: 这是我的示例代码:

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);
    }

You need to execute Where by calling ToArray or ToList . 您需要通过调用ToArrayToList执行Where You can also combine two where statements into one: 您还可以将两个where语句合并为一个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM