Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need to use a <code>DataGridViewComboBoxColumn</code>.</p> <p>There are two ways of populating the drop-down list. You can either set it manually by using the collection returned through the Items property or by binding the column to a data source through the DataSource, DisplayMember and ValueMember properties. This is the same as the WinForms ComboBox control.</p> <p>An example of setting you data source programatically is below:</p> <pre><code>public partial class Form1 : Form { public Form1() { InitializeComponent(); dataGridView1.AutoGenerateColumns = false; List&lt;User&gt; users = new List&lt;User&gt;(); users.Add(new User(){Name = "Fred", Id = 1}); users.Add(new User(){Name = "Jill", Id = 2}); users.Add(new User(){Name = "Bob", Id = 3}); List&lt;Account&gt; accounts = new List&lt;Account&gt;(); accounts.Add(new Account(){AccountName = "Mr Smith", UserId = 1}); accounts.Add(new Account() { AccountName = "Ms Brown", UserId = 2 }); accounts.Add(new Account() { AccountName = "Mr Smith 2", UserId = 1 }); dataGridView1.DataSource = accounts; DataGridViewTextBoxColumn col1 = dataGridView1.Columns[1] as DataGridViewTextBoxColumn; col1.DataPropertyName = "AccountName"; DataGridViewComboBoxColumn col = dataGridView1.Columns[0] as DataGridViewComboBoxColumn; col.DataSource = users; col.DisplayMember = "Name"; col.DataPropertyName = "UserId"; col.ValueMember = "Id"; } } public class User { public string Name { get; set; } public int Id { get; set; } } public class Account { public string AccountName { get; set; } public int UserId { get; set; } } </code></pre> <p>That assumes that in the designer you added a DataGridViewComboBoxColumn as the first column and a DataGridViewTextBoxColumn as the second column.</p> <p>What you should see is that the combo box has the three users available. The DataGridView should have three rows from the data source, for the three Accounts, and the right combo box value shoud be selected for each.</p> <p>A resource you will find very handy for any DataGridView development is the <a href="http://www.windowsclient.net/Samples/Go%20To%20Market/DataGridView/DataGridView%20FAQ.doc" rel="nofollow noreferrer">DataGridView Faq</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload