Sure; just set the DataSource, DisplayMember and ValueMember of a
DataGridViewCom boBoxColumn (below [using C# 3 for brevity]).
Marc
using System.Collecti ons.Generic;
using System.Windows. Forms;
using System.Componen tModel;
using System;
class Role
{
public string Name { get; set; }
public string Description { get; set; }
}
class User
{
public string Name { get; set; }
public string Role { get; set; }
}
static class Program
{
[STAThread]
static void Main()
{
List<Roleroles = new List<Role>
{
new Role {Name="ADMIN", Description="Ad ministrator"},
new Role {Name="GUEST", Description="Gu est"},
new Role {Name="USER", Description="St andard User"}
};
BindingList<Use rusers = new BindingList<Use r>
{
new User {Name = "Fred", Role="ADMIN"},
new User {Name="Jo", Role="GUEST"}
};
Application.Run (new Form
{
Text = "DataGridVi ew Demo",
Controls =
{
new DataGridView {
Dock = DockStyle.Fill,
AutoGenerateCol umns = false,
Columns = {
new DataGridViewTex tBoxColumn {
DataPropertyNam e = "Name",
HeaderText = "Name"
}, new DataGridViewCom boBoxColumn {
DataPropertyNam e = "Role",
DataSource = roles,
DisplayMember = "Descriptio n",
ValueMember = "Name"
}
},
DataSource = users
}
}
});
}
}
DataGridViewCom boBoxColumn (below [using C# 3 for brevity]).
Marc
using System.Collecti ons.Generic;
using System.Windows. Forms;
using System.Componen tModel;
using System;
class Role
{
public string Name { get; set; }
public string Description { get; set; }
}
class User
{
public string Name { get; set; }
public string Role { get; set; }
}
static class Program
{
[STAThread]
static void Main()
{
List<Roleroles = new List<Role>
{
new Role {Name="ADMIN", Description="Ad ministrator"},
new Role {Name="GUEST", Description="Gu est"},
new Role {Name="USER", Description="St andard User"}
};
BindingList<Use rusers = new BindingList<Use r>
{
new User {Name = "Fred", Role="ADMIN"},
new User {Name="Jo", Role="GUEST"}
};
Application.Run (new Form
{
Text = "DataGridVi ew Demo",
Controls =
{
new DataGridView {
Dock = DockStyle.Fill,
AutoGenerateCol umns = false,
Columns = {
new DataGridViewTex tBoxColumn {
DataPropertyNam e = "Name",
HeaderText = "Name"
}, new DataGridViewCom boBoxColumn {
DataPropertyNam e = "Role",
DataSource = roles,
DisplayMember = "Descriptio n",
ValueMember = "Name"
}
},
DataSource = users
}
}
});
}
}
Comment