I'm using a DataGridView
in Winforms
to display User Information and the Roles they have.
Since I would like to use the same grid to be able to UPDATE the role I change
the last DataGridViewTextBoxColumn
for a DataGridViewComboBoxColumn
and set the valueMember and DataSource
properties.
In the DataSource of the DataGridViewComboBoxColumn
I set the Context.Role.
In the DataSource of the DataGridView
I set the query with the join.
The data is displayed properly but the Combo is not working. And I get the error
datagridviewcomboboxcell value not valid.
This is my code:
var query = from u in context.User
join ur in context.UserRole on u.ID equals r.UserID
join r in context.Role on ur.RoleID.ToString() equals r.ID.ToString()
select new
{u.ID,
u.Nick,
u.LastLogin,
Role = ur == null ? String.Empty : r.Name
};
DataGridViewTextBoxColumn ColID = new DataGridViewTextBoxColumn();
DataGridViewTextBoxColumn ColNick = new DataGridViewTextBoxColumn();
DataGridViewTextBoxColumn ColLast = new DataGridViewTextBoxColumn();
DataGridViewComboBoxColumn ColRole = new DataGridViewComboBoxColumn();
userDataGridView.Columns.Add(ColID);
userDataGridView.Columns.Add(ColNick);
userDataGridView.Columns.Add(ColLast);
userDataGridView.Columns.Add(ColRole);
ColID.DataPropertyName = "ID";
ColNick.DataPropertyName = "Nick";
ColLast.DataPropertyName = "LastLogin";
ColRole.DataPropertyName = "Role";
ColRole.DataSource = context.Role.ToList();
ColRole.ValueMember = "ID";
userDataGridView.DataSource = query.ToList();
Any idea about what I'm missing?