Code: Detail

C# - (Portuguese)

Helper para personalizar e carregar um Data Grid

Este Helper facilita a configuração e carga com dados de um data grid em seu formulario webforms

Last update: 17/01/2017
 
N/A
N/A
N/A
N/A
 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Helpers.UI
{

    public class DataGridViewHelper
    {
        
        public DataGridViewHelper(System.Windows.Forms.DataGridView dataGrid, bool aplicarConfiguracoesIniciais)
        {
            dataGrid.Rows.Clear();
            dataGrid.Columns.Clear();

            if (aplicarConfiguracoesIniciais == true)
            {
                dataGrid.AllowUserToDeleteRows = false;
                dataGrid.AllowUserToOrderColumns = false;
                dataGrid.AllowUserToResizeColumns = false;
                dataGrid.AllowUserToResizeRows = false;
                dataGrid.AllowUserToAddRows = false;
                dataGrid.RowHeadersVisible = false;
                dataGrid.ReadOnly = false;
                dataGrid.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
            }
        }
        
        public class DataGridViewDTO
        {
            public string CabecalhoColuna { get; set; }
            public string ValorLinhaColuna { get; set; }
            public bool Visivel { get; set; }
            public int Width { get; set; }
            public bool Check { get; set; }
            public bool ReadyOnly { get; set; }
        }
        
        

        public void AdicionarItem(System.Windows.Forms.DataGridView dataGrid, List<DataGridViewDTO> Conteudo)
        {
            //Monta Colunas
            if (dataGrid.Rows.Count == 0)
            {
                foreach (DataGridViewDTO _dataGridViewDto in Conteudo)
                {
                    if (_dataGridViewDto.Check == false)
                    {
                        dataGrid.Columns.Add(_dataGridViewDto.CabecalhoColuna, _dataGridViewDto.CabecalhoColuna);
                        dataGrid.Columns[dataGrid.Columns.Count - 1].ReadOnly = true;
                    }
                    else
                        dataGrid.Columns.Add(new System.Windows.Forms.DataGridViewCheckBoxColumn() { HeaderText = string.Empty, ReadOnly = _dataGridViewDto.ReadyOnly });

                    dataGrid.Columns[dataGrid.Columns.Count - 1].Visible = _dataGridViewDto.Visivel;
                    if(_dataGridViewDto.Width > 0)
                        dataGrid.Columns[dataGrid.Columns.Count - 1].Width = _dataGridViewDto.Width;
                }

            }

            //Carrega Grid com valores
            object[] linhaDados = new object[Conteudo.Count];
            int indiceLinhaDados = 0;
            foreach (DataGridViewDTO _dataGridViewDto in Conteudo)
            {
                if (_dataGridViewDto.Check == false)
                    linhaDados[indiceLinhaDados] = _dataGridViewDto.ValorLinhaColuna;
                else
                    linhaDados[indiceLinhaDados] = Convert.ToBoolean(_dataGridViewDto.ValorLinhaColuna);

                indiceLinhaDados++;
            }

            dataGrid.Rows.Add(linhaDados);

            


        }
    }
}




/*************************************************
             Exemplo de utilizacao
*************************************************/


        private void CarregarGrid(List<PedidoDTO> pedidos)
        {
            try
            {
                Helpers.UI.DataGridViewHelper _dataGridViewHelper = new DataGridViewHelper(grdPedidosRoteiro, true);

                foreach (PedidoDTO _pedido in pedidos)
                {
                    string _conteudoColRoteiro = string.Empty;
                    if (_pedido.Roteiro.Key > 0)
                        _conteudoColRoteiro = _pedido.Roteiro.Key.ToString() + " - " + _pedido.Roteiro.Descricao;

                    List<DataGridViewHelper.DataGridViewDTO> _conteudoDataGridView = new List<DataGridViewHelper.DataGridViewDTO>();
                    _conteudoDataGridView.Add(new DataGridViewHelper.DataGridViewDTO() { CabecalhoColuna = " ", ValorLinhaColuna = "false", Visivel = true, Width = 100, Check = true });
                    _conteudoDataGridView.Add(new DataGridViewHelper.DataGridViewDTO() { CabecalhoColuna = "Pedido", ValorLinhaColuna = _pedido.Key.ToString(), Visivel = true, Width = 100 });
                    _conteudoDataGridView.Add(new DataGridViewHelper.DataGridViewDTO() { CabecalhoColuna = "Status", ValorLinhaColuna = _pedido.CodigoStatus.ToString(), Visivel = true, Width = 50 });
                    _conteudoDataGridView.Add(new DataGridViewHelper.DataGridViewDTO() { CabecalhoColuna = "Cod.Roteiro", ValorLinhaColuna = _pedido.Roteiro.Key.ToString(), Visivel = false, Width = 100 });
                    _conteudoDataGridView.Add(new DataGridViewHelper.DataGridViewDTO() { CabecalhoColuna = "Roteiro", ValorLinhaColuna = _conteudoColRoteiro, Visivel = true, Width = 200 });
                    _conteudoDataGridView.Add(new DataGridViewHelper.DataGridViewDTO() { CabecalhoColuna = "Região", ValorLinhaColuna = _pedido.Roteiro.Regiao.Descricao, Visivel = true, Width = 220 });

                    _dataGridViewHelper.AdicionarItem(grdPedidosRoteiro, _conteudoDataGridView);

                }
            }
            catch (Exception ex)
            {
                this.TratarErro(ex);
            }
        }

Source:
 
Users who have marked this routine as a favorite
 
 
 
The site ti4fun is not responsible for the content on sites for which you have external links

Articles, routines, tips, forums, blogs or any other content posted on ti4fun site is not tested and not validated, so you should test and validate any information collected on the ti4fun site before applying it to final use environment, such as example, production. the TI4FUN site is not responsible for quality or for any damages, direct, indirect or consequential, from use of any content posted by the authors in the site.

All content published on the ti4fun site is the responsibility of the author and do not necessarily express the views of the site ti4fun and its employees.