Ir para conteúdo

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Jonatã Cioni

Problema ao carregar o DataGridView Windows Form C#

Recommended Posts

bom galera... posso estar sendo muito noob, posso estar fazendo alguma bobeira sem saber, mas quando eu atribuo o DataTable ao DataSource do DataGridView, atribui certo, mas na tela, aparece todas as linhas vazias...

 

código:

private void CarregaGridPermissoes()
{
	try
	{
		Int32 idGrupo = Int32.Parse(this.cbGruposPermissoes.SelectedValue.ToString());
		List<SysMenu> listaMenu = new SysMenuFacade().Listar();
		List<Permissao> listaPermissoes = new PermissaoFacade().Filtrar(new Permissao()
		{
			GrupoId = idGrupo
		});
		DataTable dtGrupoPerms = new DataTable();
		String[] Columns = new String[] { "MenuNome", "P_Acesso", "P_Consulta", "P_Insercao", "P_Edicao", "P_Exclusao" };
		Type[] Types = new Type[] { typeof(String), typeof(Boolean), typeof(Boolean), typeof(Boolean), typeof(Boolean), typeof(Boolean) };
		for (Int32 i = 0; i < Columns.Length; i++)
		{
			dtGrupoPerms.Columns.Add(Columns[i], Types[i]);
		}
		foreach (SysMenu menu in listaMenu)
		{
			DataRow row = dtGrupoPerms.NewRow();
			List<Permissao> listaFiltro = listaPermissoes.FindAll(
				p => p.MenuId == menu.Id && p.GrupoId == idGrupo
			).OrderBy(x => x.TipoPermissaoId).ToList();
				row["MenuNome"] = menu.Nome;
			foreach (Permissao p in listaFiltro)
			{
				row[Columns[p.TipoPermissaoId]] = p.Status == Permissao.ALLOW ? true : false;
			}
			dtGrupoPerms.Rows.Add(row);
		}
		this.gvGruposPermissoes.AutoGenerateColumns = false;
		this.gvGruposPermissoes.DataSource = dtGrupoPerms;
	}
	catch(Exception ex)
	{
		this.ShowMessage(ex);
	}
}

quando eu tiro o "this.gvGruposPermissoes.AutoGenerateColumns = false;" ele mostra, só que em outras colunas ao lado, ele gera outras colunas

 

Código do arquivo Design.cs:

this.gvGruposPermissoes.AllowUserToAddRows = false;
this.gvGruposPermissoes.AllowUserToDeleteRows = false;
this.gvGruposPermissoes.AllowUserToResizeColumns = false;
this.gvGruposPermissoes.AllowUserToResizeRows = false;
this.gvGruposPermissoes.BackgroundColor = System.Drawing.SystemColors.Control;
this.gvGruposPermissoes.CellBorderStyle = System.Windows.Forms.DataGridViewCellBorderStyle.SingleHorizontal;
this.gvGruposPermissoes.ColumnHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.Single;
this.gvGruposPermissoes.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.gvGruposPermissoes.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.MenuNome,
            this.P_Acesso,
            this.P_Consulta,
            this.P_Insercao,
            this.P_Edicao,
            this.P_Exclusao});
this.gvGruposPermissoes.EnableHeadersVisualStyles = false;
this.gvGruposPermissoes.Location = new System.Drawing.Point(6, 62);
this.gvGruposPermissoes.MultiSelect = false;
this.gvGruposPermissoes.Name = "gvGruposPermissoes";
this.gvGruposPermissoes.RowHeadersVisible = false;
this.gvGruposPermissoes.RowHeadersWidthSizeMode = System.Windows.Forms.DataGridViewRowHeadersWidthSizeMode.DisableResizing;
this.gvGruposPermissoes.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
this.gvGruposPermissoes.ShowEditingIcon = false;
this.gvGruposPermissoes.Size = new System.Drawing.Size(523, 352);

// 
// MenuNome
// 
dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
this.MenuNome.DefaultCellStyle = dataGridViewCellStyle1;
this.MenuNome.HeaderText = "Menu";
this.MenuNome.Name = "MenuNome";
this.MenuNome.ReadOnly = true;
this.MenuNome.Resizable = System.Windows.Forms.DataGridViewTriState.False;
this.MenuNome.Width = 203;
// 
// P_Acesso
// 
this.P_Acesso.HeaderText = "Acesso";
this.P_Acesso.Name = "P_Acesso";
this.P_Acesso.ReadOnly = true;
this.P_Acesso.Resizable = System.Windows.Forms.DataGridViewTriState.False;
this.P_Acesso.Width = 60;
// 
// P_Consulta
// 
this.P_Consulta.HeaderText = "Consulta";
this.P_Consulta.Name = "P_Consulta";
this.P_Consulta.ReadOnly = true;
this.P_Consulta.Resizable = System.Windows.Forms.DataGridViewTriState.False;
this.P_Consulta.Width = 60;
// 
// P_Insercao
// 
this.P_Insercao.HeaderText = "Inserção";
this.P_Insercao.Name = "P_Insercao";
this.P_Insercao.ReadOnly = true;
this.P_Insercao.Resizable = System.Windows.Forms.DataGridViewTriState.False;
this.P_Insercao.Width = 60;
// 
// P_Edicao
// 
this.P_Edicao.HeaderText = "Edição";
this.P_Edicao.Name = "P_Edicao";
this.P_Edicao.ReadOnly = true;
this.P_Edicao.Resizable = System.Windows.Forms.DataGridViewTriState.False;
this.P_Edicao.Width = 60;
// 
// P_Exclusao
// 
this.P_Exclusao.HeaderText = "Exclusão";
this.P_Exclusao.Name = "P_Exclusao";
this.P_Exclusao.ReadOnly = true;
this.P_Exclusao.Resizable = System.Windows.Forms.DataGridViewTriState.False;
this.P_Exclusao.Width = 60;

Compartilhar este post


Link para o post
Compartilhar em outros sites

eu tenho outro sistema que tem escrito o codigo deste mesmo jeito e funciona... não sei pq nesse especifico não funciona... mas já dei um jeito aki... fiz essas atribuições de colunas manualmente mesmo... pq o cliente esta com pressa pra ter o sistema em mãos... vlw pela tentativa de ajuda, brother...

Compartilhar este post


Link para o post
Compartilhar em outros sites

qjuando esta propriedade esta ture ela define um valor que indica se campos associados são criados automaticamente para cada campo da fonte de dados, se tiver como false, você pode manualmente definir os campos de coluna definindo a propriedade de AutoGenerateColumns a false e então criar uma coleção personalizado de Columns. Pode-se combinar campos de coluna explicitamente declarados com os campos geradas automaticamente de coluna. Quando ambas são usadas, colunas explicitamente declaradas são processadas primeiro, seguidas das colunas geradas automaticamente. Os campos geradas automaticamente de coluna associada não são adicionados à coleção de columns

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.