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

264 views Asked by At

This is my first question here on the stack overflow, sorry if I did something wrong !!

I'm starting with C# and DevExpress and I'm trying to create an example using the DevExpress GridControl (v.19.2.5.0) with a class that contains some records in a List.

I created a simple project (Windows Forms) and put a GridControl and tried to connect the GridControl to the class (Record.cs/PrototipoViewModel.cs).

The GridControl in designer mode, lists the columns, but in run-time, it is empty.

Where did I go wrong? Or what did I not do to list the records in run-time?

My second question would be, can I insert, update and delete the records in run-time directly in GridControl using a list ?

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

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

The project is on in GitHub ( https://github.com/tiago-pimenta/vs_gridcontrol_bind_class )

Thanks

1

There are 1 answers

0
tiago_pimenta On

Well folks, I was able to find where the error was in my code, I thought that setting the "Choose DataSource" property of GridControl would be enough, but the code below was missing:

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

Well, now I just have to solve the problem of allowing GridControl to insert new lines, reading the DevExpress documentation, says to set the "AllowNew" and "AllowRemove" properties to True from the DataSource, and also says that you need to create a class with one property of this required simple type. This class must have an empty default constructor.

My DataSource, only has the property "AllowNew" and is already set to "True", but even so I can't insert new lines in the GridControl.

Is my class the property of this required simple type? Does it have the default constructor empty?

Thank you who helped so far !!!