0

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?

dubs
  • 6,511
  • 4
  • 19
  • 35

1 Answers1

0

First before you set the gridview datasource add the unbound comboboxColumn give it a name, then set datasource,then set the datagridview datasource .

    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();

ColID.DataPropertyName = "ID";
ColNick.DataPropertyName = "Nick";
ColLast.DataPropertyName = "LastLogin";

ColRole.DataPropertyName = "Role";
ColRole.DataSource = context.Role.ToList();
ColRole.ValueMember = "ID";

userDataGridView.Columns.Add(ColID);
userDataGridView.Columns.Add(ColNick);
userDataGridView.Columns.Add(ColLast);
userDataGridView.Columns.Add(ColRole);

userDataGridView.DataSource = query.ToList();
M. Wiśnicki
  • 6,094
  • 3
  • 23
  • 28