Yazdgerd Sasani

Yazdgerd Sasani

  • NA
  • 434
  • 698

How to add forigen key in datagridview with combobox?

Jul 17 2015 5:00 AM

I have 2 class:

public class Users
    {
        public Users() { }
        [Key]
        public int UserId { getset; }
        public string FirstName { getset; }
        public string LastName { getset; }
        public string UserName { getset; }
        public string Description { getset; }
        public int GroupID { getset; }
        //[ForeignKey("GroupID")]
        public virtual Groups Groups { getset; }
    }
public class Groups
    {
        public Groups() { }
        [Key]
        public int GroupID { getset; }
        [Required]
        public string  GroupName { getset; }
        public string GroupDescription { getset; }
 
        public virtual ICollection<Users> Users { getset; }
    }

I want used datagridview in winform for editing user and I want used GroupID col in datagridview with combobox and show Group record

anybody can help me? how can do that?
 
_context = new ApplicationContext();
            var query = (from o in _context.Users
                         select o).ToList();
            bi = new BindingSource();
            var columnCombo = new DataGridViewComboBoxColumn();
            columnCombo.DataSource = _context.Groups.ToList();
            columnCombo.DisplayMember = "GroupName";
            columnCombo.ValueMember = "GroupID";
            columnCombo.HeaderText = "????";
            columnCombo.ReadOnly = false;
 
bi.DataSource = _context.Users.ToList();
            
            dataGridViewUser.DataSource = bi;
            
            dataGridViewUser.Columns[0].ReadOnly = true;
 
dataGridViewUser.Columns.Add(columnCombo);