How can I create a DataGridView with Mongodb?

0

I want to display the DB data in a table, but so far I have been able to do it with a ListView . How can I do this but using a 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)
        {

        }


    }
}
    
asked by anonymous 05.12.2014 / 04:56

2 answers

0

My problem can be solved in two ways. Here is the code for two dataGridViews each with 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 ...

    
06.12.2014 / 20:15
1

Assuming the name of DataGridView is 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
}
  

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

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

    
06.12.2014 / 16:25