How can I create a Datagridview with Mongodb?

Asked

Viewed 243 times

0

I want to present the BD data in a table, but so far I could only do it using a ListView. How can I do it but using one DataGridView?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using MongoDB.Driver.Linq;

namespace WFRecibos
{
    public partial class frmListaClientes : Form
    {
        public frmListaClientes()
        {
            InitializeComponent();
        }


            public IEnumerable<Cliente> getTodosClientes()
            {

                var colClientes = DbHelper.getCollection("Cliente");

                var clienteLista = from e in colClientes.AsQueryable<Cliente>() select e;

                return clienteLista;
            }

        private void frmListaCli_Load(object sender, EventArgs e)
        {
            var consulta = this.getTodosClientes();
            listView1.Items.Clear();
                foreach (var cli in consulta)
                {
                    listView1.Items.Add(cli.Nome);
                    listView1.Items.Add(cli.Municipio);
                    listView1.Items.Add(cli.Corrego);
                }
        }

        private void gdvClientes_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }


    }
}
  • You already have some code of this integration with Mongodb?

  • already have the drivers for C# and I have the query to Collection returning all customer data in a variable 'query'.

  • @Edit your question and put the code you have using the ListView, this makes it easier for other people to help you, you can follow the tips of that link

  • entered my code... but today I was able to list the names of the 'columns' of Collection... only that did not return the data saved in DB

  • @Enrique, I answered your question, regarding the problem you mentioned in the comments, create a new question to try to solve it, because I believe that this problem escapes from the original context of your question

2 answers

1

Supposing the name of DataGridView be it grdDados, you can do the following:

private void frmListaCli_Load(object sender, EventArgs e)
{
    var consulta = this.getTodosClientes();
    grdDados.DataSource = consulta;

    grdDados.Columns["Municipio"].HeaderText = "Município"; // altera cabeçalho da coluna
    grdDados.Columns["Corrego"].HeaderText = "Córrego"; // altera cabeçalho da coluna
}

Observing: I also put in the code an option to change the column header, you can use if you think necessary.

If you wanted more information about the property DataSource, you can take a look at that link.

  • if I replace my code with yours, give me a blank dataGridView..

  • but using the ListView the information is displayed?

  • are yes. Then I checked the link you passed, and in the first example came my answer, tested here now. Now is to figure out how to create 2 buttons for edit and delete actions... I am adventurous in C# work with PHP, then I know what I want but I don’t know how to do some things yet..

  • in a test I performed here with Windows Forms worked normally, if you managed to solve your problem, answer your question by saying how you did it and what you used, it may help other users in the future

0


My problem can be solved in two ways. Follow the code of two dataGridView each presenting a solution:

private BindingSource bindingSource1 = new BindingSource();

        private void frmListaCli_Load(object sender, EventArgs e)
        {
            //-------> gdvClientes   -----------------------------------
            try
            {
                gdvClientes.AutoGenerateColumns = true;

                bindingSource1.DataSource = getTodosClientes();
                gdvClientes.DataSource = bindingSource1;
                gdvClientes.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;
                gdvClientes.BorderStyle = BorderStyle.Fixed3D;
                gdvClientes.EditMode = DataGridViewEditMode.EditOnEnter;
            }
            catch (Exception)
            {
                MessageBox.Show("To run this sample replace connection.ConnectionString" +
                    " with a valid connection string to a Northwind" +
                    " database accessible to your system.", "ERROR",
                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                System.Threading.Thread.CurrentThread.Abort();

            }



            //-------> dataGridView1   -----------------------------------

            DataTable dt = new DataTable();

            dt.Columns.Add("Nome");
            dt.Columns.Add("Corrego");
            dt.Columns.Add("Municipio");
            dt.Columns.Add("Ações");

            var consulta = this.getTodosClientes();
            foreach (var cli in consulta)
            {

                dt.Rows.Add(

                             cli.Nome,

                             cli.Corrego,

                             cli.Municipio,
                             "Editar - Excluir"

                             );




            } dataGridView1.DataSource = dt;

        }

Valew @Mateus Alexandre for helping me...

  • now you can accept the answer as it is on tour

Browser other questions tagged

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