C# - Connect Gridcontrol with a List and manipulate the records in run-time

Asked

Viewed 34 times

-1

all right ???

This is my first question here on stack overflow, I’m sorry if I did anything wrong !!!

I’m starting with C# and Devexpress and I’m trying to create an example using 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 Gridcontrol to the class (Record.Cs / Prototipoviewmodel.Cs).

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

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

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

Class Record.Cs

using System;
using System.Collections.Generic;

namespace Prototipo
{
    Registro de classe pública
    {
        public DateTime? Dados {get; definir; }
        public string Cliente {get; definir; }
        public string Movimento {get; definir; }
        Valor decimal público {get; definir; }

        public static List <Record> GetRecords ()
        {
            Lista <Registro> pessoas = nova Lista <Registro> ();

            people.Add (new Record () {Data = new DateTime (2021, 04, 07, 19, 00, 00), Cliente = "João", 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});

            retornar pessoas;
        }
    }
}

Class Prototypoviewmodel.Cs

using System.Collections.Generic;

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

        Public List <Record> Records {get; definir; }
    }
}

and Class frmPrototypMain.Cs

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

namespace Prototipo
{
    classe pública parcial frmPrototipoMain: DevExpress.XtraEditors.XtraForm
    {
        public frmPrototipoMain ()
        {
            InitializeComponent ();
        }
    }
}

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

Thank you

  • hello, I didn’t see in your question where does the bind of your data on the Grid

  • @Ricardopunctual, in the "Data Sources" tab, I created one of the type "Object" and pointed to the class "Prototipoviewmodel" and in the property "Choose Datasource" of Grid, I selected "recordsBindingSource". It was just after this process that the columns appeared in design mode, but did not bring the data.

  • and you called him DataBind() right?

  • @Ricardopunctual no, the only codes really are the ones I put in, plus this configuration I ended up doing in Gridcontrol. I already used Devexpress in Delphi, there to popular the grid, we gave an "Open" in Dataset/Datasource, I believe that is missing a similar command, to give the "Get" in the records of the class Record.Cs, only I do not know how to do it.

1 answer

0

Good people, I was able to find where was the error in my code, I thought that setting the property "Choose Datasource" of Gridcontrol, sufficed, but lacked the code below:

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

Well, now I can only solve the problem of allowing Gridcontrol to insert new lines, reading the Devexpress documentation, says to set the properties "Allownew" and "Allowremove" to True from Datasource, and also says that you need to create a class with a required simple type property and that the class should have an empty default constructor.

My Datasource only has the "Allownew" property and is already set to "True", but I still can’t insert new lines in Gridcontrol.

My class is the required simple type ? It has the standard empty constructor ?

Thank you who helped so far !!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.