C# - 将 GridControl 绑定到 List 并在运行时操作记录

问题描述

这是我关于堆栈溢出的第一个问题,对不起,如果我做错了什么!!

我从 C# 和 DevExpress 开始,我正在尝试使用 DevExpress GridControl (v.19.2.5.0) 和一个包含列表中的一些记录的类创建一个示例。

我创建了一个简单的项目(Windows 窗体)并放置了一个 GridControl 并尝试将 GridControl 连接到类 (Record.cs/Prototipoviewmodel.cs)。

设计器模式下的 GridControl,列出了列,但在运行时,它是空的。

我哪里做错了?或者我没有做什么来在运行时列出记录?

我的第二个问题是,我可以在运行时使用列表直接在 GridControl 中插入、更新和删除记录吗?

类记录.cs

using System;
using System.Collections.Generic;

namespace Prototipo
{
    public class Record
    {
        public DateTime? Data { get; set; }
        public string Cliente { get; set; }
        public string Movimento { get; set; }
        public decimal Valor { get; set; }

        public static List<Record> GetRecords()
        {
            List<Record> people = new List<Record>();

            people.Add(new Record() { Data = new DateTime(2021,04,07,19,00,00),Cliente = "Joao",Movimento = "D",Valor = 1000});
            people.Add(new Record() { Data = new DateTime(2021,30,Cliente = "Maria",Valor = 2000 });
            people.Add(new Record() { Data = new DateTime(2021,20,Cliente = "Jose",Valor = 3000 });

            return people;
        }
    }
}

类 Prototipoviewmodel.cs

using System.Collections.Generic;

namespace Prototipo
{
    public class Prototipoviewmodel
    {
        public Prototipoviewmodel()
        {
            this.Records = Record.GetRecords();
        }

        public List<Record> Records { get; set; }
    }
}

和类 frmPrototipoMain.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Prototipo
{
    public partial class frmPrototipoMain : DevExpress.XtraEditors.XtraForm
    {
        public frmPrototipoMain()
        {
            InitializeComponent();
        }
    }
}

该项目在 GitHub 上 (https://github.com/tiago-pimenta/vs_gridcontrol_bind_class)

谢谢

解决方法

伙计们,我能够在我的代码中找到错误的位置,我认为设置 GridControl 的“选择数据源”属性就足够了,但是缺少下面的代码:

private void frmPrototypeMain_Load (object sender,EventArgs e)
         {
             gridControl.DataSource = Prototipo.Record.GetRecords();
         }

好吧,现在我只需要解决允许GridControl插入新行的问题,阅读DevExpress文档,说要从DataSource中将“AllowNew”和“AllowRemove”属性设置为True,还说你需要创建一个具有此所需简单类型的属性的类。这个类必须有一个空的默认构造函数。

我的数据源只有属性“AllowNew”并且已经设置为“True”,但即使如此我也无法在 GridControl 中插入新行。

我的类是这个必需的简单类型的属性吗?它的默认构造函数是否为空?

感谢到目前为止提供帮助的人!!!