簡體   English   中英

C# - 將 GridControl 綁定到 List 並在運行時操作記錄

[英]C# - Binding GridControl to List and manipulating the records in run-time

這是我關於堆棧溢出的第一個問題,如果我做錯了什么對不起!

我從 C# 和 DevExpress 開始,我正在嘗試使用 DevExpress GridControl (v.19.2.5.0) 和 class 創建一個示例,其中包含列表中的一些記錄。

我創建了一個簡單的項目(Windows 窗體)並放置了一個 GridControl,並嘗試將 GridControl 連接到 class(Record.cs/PrototipoViewModel.cs)。

設計器模式下的 GridControl 列出了列,但在運行時,它是空的。

我在哪里 go 錯了? 或者我沒有做什么來在運行時列出記錄?

我的第二個問題是,我可以在運行時使用列表直接在 GridControl 中插入、更新和刪除記錄嗎?

Class Record.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, 04, 07, 19, 30, 00), Cliente = "Maria", Movimento = "D", Valor = 2000 });
            people.Add(new Record() { Data = new DateTime(2021, 04, 07, 20, 00, 00), Cliente = "Jose", Movimento = "D", Valor = 3000 });

            return people;
        }
    }
}

Class PrototipoViewModel.cs

using System.Collections.Generic;

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

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

和 Class 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,還說你需要創建一個class 具有此所需簡單類型的一個屬性。 這個 class 必須有一個空的默認構造函數。

我的數據源只有屬性“AllowNew”並且已經設置為“True”,但即便如此我也無法在 GridControl 中插入新行。

我的 class 是這個必需的簡單類型的屬性嗎? 它的默認構造函數是否為空?

謝謝你到目前為止的幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM