I create a DataGridView as follows
private void IniciarGV()
{
using (var db = new Entities())
{
var ListaPantallas = (from a in db.PANTALLAS
select a).ToList();
if (ListaPantallas != null)
{
gvPermisos.DataSource = ListaPantallas;
gvPermisos.Columns["idPantalla"].Visible = false;
gvPermisos.Columns["nombrepantalla"].HeaderText = "Nombre";
//gvPermisos.Columns.Add(new DataGridViewCheckBoxColumn());
DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn();
checkBoxColumn.HeaderText = "Seleccione";
checkBoxColumn.Width = 50;
checkBoxColumn.Name = "checkBoxColumn";
gvPermisos.Columns.Insert(0, checkBoxColumn);
//gvPermisos.EndEdit();
}
db.Dispose();
}
}
After that a i retrieve the privileges with the same method linq
var TraerPermisos = (from a in db.PERMISOS
where a.IDUSUARIO == UsuarioEditar.idUsuario
select a).ToList();
After i go through the gridview to match the privileges with the ids that i wanted to check so the user can edit them, but for some reason they always appear unmarked
so far this is what i encounter in every forum or google, but it doesnt seem to work
foreach (PERMISOS item in TraerPermisos)
{
foreach (DataGridViewRow row in gvPermisos.Rows)
{
var ValorPermisoEnGV = Convert.ToBoolean(row.Cells["checkBoxColumn"].Value);
var ValorPantalla = decimal.Parse(row.Cells["idPantalla"].Value.ToString());
if (ValorPantalla == item.IDPANTALLA)
{
row.Cells["checkBoxColumn"].Value = true;
}
}
}
I load the privileges on start
public ManejarUsuario()
{
InitializeComponent();
IniciarComboBox();
IniciarGV();
if (UsuarioEditar.idUsuario != 0)
{
CargarDatos();
btnCrearUsuario.Text = "Editar";
CargarPrivilegios();
}
}
Sorry about the long post, i've tried so many options but none seems to work
CodePudding user response:
Life would be a lot easier if you just put a Boolean in the data driving the grid. First, let's make a holder class for your info
class Perms{
public int IdPantalla { get; set; }
public string NombrePantalla { get; set; }
public bool HasPermission { get; set; }
}
Then let's have the query produce a list of these instead:
//get all the IDPANTALLA this user has and put in a hash set
var TraerPermisos = (from a in db.PERMISOS
where a.IDUSUARIO == UsuarioEditar.idUsuario
select a.IDPANTALLA).ToHashSet();
//make a list of objects that includes a bool of whether the user has that permission
//note you should have this be a class level variable for ease of use later/elsewhere
ListaPantallas = (from a in db.PANTALLAS
select new Perms {
IdPantalla a.IdPantalla,
HasPermission = TraerPermisos.Contains(a.idPantalla) ,
NombrePantalla = a.nombrepantalla
}).ToList();
Then set up the grid:
gvPermisos.DataSource = ListaPantallas;
gvPermisos.Columns["IdPantalla"].Visible = false;
gvPermisos.Columns[ "NombrePantalla"].HeaderText = "Nombre";
gvPermisos.Columns[ "HadPermission"].HeaderText = "Seleccione";
The grid will find the bool property in the list and wire it up for you as a check column. It doesn't need you to make a column for it. Anything you tick is then stored in the underlying list as a true/false and you can eg foreach(var x in ListaPantallas) if(x.HasPermission...
When manipulating data in a DGV that is bound to a data source, manipulate the source. Also consider to make Perms implements INotifyPropertyChanged (and consider switching to using a binding list)