简体   繁体   English

将列表项显示到数据网格视图中 C#

[英]display list items into data grid view c#

I have a super-class (abstract) and then 2 inherited classes.我有一个超类(抽象),然后是 2 个继承类。
SuperClass: Sessions超类:会话
Class 1: Cycling第 1 类:骑自行车
Class 2: Running第 2 课:跑步

I also have a list that will hold all of my objects我还有一个包含我所有对象的列表
private List<Session> allSessions = new List<Session>();

I also have declared some arrays that hold hard-coded data to populate my objects.我还声明了一些包含硬编码数据的数组来填充我的对象。

Also, Running and Cycling has an overridden ToString() method that displays different data depending on the class.此外,Running and Cycling 有一个重写的ToString()方法,该方法根据类显示不同的数据。

public override string ToString() => $"Cycle Average RPM is {AverageRpm} and Average Resistance is {AverageResistance}";

I am using a for loop to create and add new objects into my list like this我正在使用 for 循环来创建新对象并将其添加到我的列表中,如下所示

            for (int i = 0; i < id.Length; i++)
            {
                Cycling Cycle = new Cycling(id[i], titles[i], date[i], duration[i], difficulty[i], instructor[i],
                    description[i], averageRpm[i], averageResistance[i]);

                // Add new objects to list
                allSessions.Add(Cycle);
            }    

I have a dataGridView that is getting everything from my list and displays it like this:我有一个 dataGridView,它从我的列表中获取所有内容并像这样显示它: 在此处输入图片说明

My problem now is, that I want to display only specific data depending on what you choose in the ComboBox, but something is not working,我现在的问题是,我想根据您在 ComboBox 中选择的内容只显示特定数据,但有些东西不起作用,

The overridden ToString() is not added to the list for some reason and whenever I choose a different option from the ComboBox, nothing is being displayed.由于某种原因,覆盖的ToString()没有添加到列表中,每当我从 ComboBox 中选择不同的选项时,都不会显示任何内容。

EDIT 1:编辑 1:

            // Filter Sessions by type using Linq
        var sessions = new List<Session>();
        var cyclingSessions = sessions.OfType<Cycling>();
        var runningSessions = sessions.OfType<Running>();

        listBox1.DataSource = null;
        listBox1.Items.Clear();


        if (cboMenu.SelectedIndex == 0)
        {
            // Populate GridView with data
            dataDisplay.DataSource = allSessions;
        }
        else if (cboMenu.SelectedIndex == 1)
        {
            // Populate GridView with data
            dataDisplay.DataSource = cyclingSessions;
        }
        else
        {
            // Populate GridView with data
            dataDisplay.DataSource = runningSessions;
        }
    }

You need to filter your sessions list and set that as your data source you can easily filter the list using OfType from System.Linq It would look something like this:您需要筛选会话列表,并设置为您的数据源,你可以轻松过滤使用列表OfTypeSystem.Linq它看起来是这样的:

var sessions = new List<Sessions>();
var cyclingSessions = sessions.OfType<Cycling>();
var runningSessions = sessions.OfType<Running>();
dataDisplay.DataSource = cyclingSessions;

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

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