C# 如何在winforms中将列表内容显示到DataGridView中

C# 如何在winforms中将列表内容显示到DataGridView中,c#,winforms,list,datagridview,C#,Winforms,List,Datagridview,我的问题很简单。我正在为一个学校项目制作日历。我需要将某些属性添加到对象列表中,以便将事件保存到日历中。我已经成功地向列表中添加了一些内容,但是我还没有找到一种在Winforms中显示列表中所有事件的方法。我的教授建议我使用DataGridView来显示列表的内容。 有人能告诉我如何将列表中的对象绑定到DataGridView,并且每当我向列表中添加内容时,它也会将其添加到网格中吗?我们将不胜感激 以下是我到目前为止的情况: public static List<Event>

我的问题很简单。我正在为一个学校项目制作日历。我需要将某些属性添加到对象列表中,以便将事件保存到日历中。我已经成功地向列表中添加了一些内容,但是我还没有找到一种在Winforms中显示列表中所有事件的方法。我的教授建议我使用DataGridView来显示列表的内容。 有人能告诉我如何将列表中的对象绑定到DataGridView,并且每当我向列表中添加内容时,它也会将其添加到网格中吗?我们将不胜感激

以下是我到目前为止的情况:

    public static List<Event> events = new List<Event>();

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }

    public static void addEvent(string eventDate, string eventTitle, string eventInfo)
    {
        Event Event = new Event();
        Event.eventDate = eventDate;
        Event.eventDate = eventTitle;
        Event.eventInfo = eventInfo;
        events.Add(Event);
    }

}


class Event
{
    public string eventDate { get; set; }
    public string eventTitle { get; set; }
    public string eventInfo { get; set; }
}
公共静态列表事件=新列表();
[状态线程]
静态void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(新Form1());
}
公共静态void addEvent(字符串eventDate、字符串eventTitle、字符串eventInfo)
{
事件=新事件();
Event.eventDate=eventDate;
Event.eventDate=eventTitle;
Event.eventInfo=eventInfo;
事件。添加(事件);
}
}
班级活动
{
公共字符串eventDate{get;set;}
公共字符串eventTitle{get;set;}
公共字符串eventInfo{get;set;}
}

}

将DataGridView绑定到绑定列表

代码:

publicstaticvoidaddevent(stringeventdate、stringeventtitle、stringeventinfo)
{
事件=新事件();
Event.eventDate=eventDate;
Event.eventDate=eventTitle;
Event.eventInfo=eventInfo;
事件。添加(事件);
var bindinglist=新的bindinglist(事件);
var source=新的BindingSource(bindingList,null);
grid.DataSource=source;
}

您不需要做任何特殊的事情:

 Event event= new Event();
    event.eventDate = eventDate;
    event.eventDate = eventTitle;
    event.eventInfo = eventInfo;
    events.Add(event);
DataGridView1.DataSource = events;

但是请验证您的事件类是否定义为public类

好的,谢谢,但是我如何使我的Program.CS类和Form.CS类可以这样做……我的data GridView1不在Program.CS类的scpoe中。最后,我希望列表框只显示事件标题…这方面的帮助将非常有用。为什么您必须在Programm类而不是Form类中执行此操作?打开表单设计器,添加DataGridView。在窗体的OnLoad事件处理程序上,将列表设置为数据源…Programm类的目标是启动winForm应用程序。在表格中完成你的工作!好的,谢谢,但是我怎样才能在Program.CS类和Form.CS类中实现这一点……我的data GridView1不在Program.CS类的scpoe中。最后,我希望列表框只显示事件标题…在此方面的帮助将不胜感激
 Event event= new Event();
    event.eventDate = eventDate;
    event.eventDate = eventTitle;
    event.eventInfo = eventInfo;
    events.Add(event);
DataGridView1.DataSource = events;