Hi,
DataGridViewComboBoxColumn does support Guid type as ValueMember. But the underline datasource should declear that column as Guid type. Otherwise you will get error during binding process. Please look at the following sample.
public partial class Form1 : Form
{
private List<MyObject> lstObj = new List<MyObject>();
private DataTable dtSource = new DataTable();
public Form1()
{
InitializeComponent();
dtSource.Columns.Add("Column1");
dtSource.Columns[0].DataType = typeof(Guid); Guid gu1 = Guid.NewGuid();
Guid gu2 = Guid.NewGuid();
Guid gu3 = Guid.NewGuid();
dtSource.Rows.Add(gu1);
dtSource.Rows.Add(gu2);
dtSource.Rows.Add(gu3);
lstObj.Add(new MyObject(gu1, "Item1"));
lstObj.Add(new MyObject(gu2, "Item2"));
lstObj.Add(new MyObject(gu3, "Item3"));
DataGridViewComboBoxColumn dgvCmb = new DataGridViewComboBoxColumn();
dgvCmb.DataSource = lstObj;
dgvCmb.DisplayMember = "Name";
dgvCmb.ValueMember = "ID";
dgvCmb.DataPropertyName = "Column1";
dataGridView1.Columns.Add(dgvCmb);
dataGridView1.DataSource = dtSource;
}
}
public class MyObject
{
private Guid id;
public Guid ID
{
get { return id; }
set { id = value; }
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
public MyObject(Guid id, string name)
{
this.id = id;
this.name = name;
}
}
From the above sample you can see that ID property is a Guid type. I have declear the DataType of Column1 of dtSource as Guid type. The code can be run properly. Another information that should be noticed is if you add a new Guid to the Column1 which doesn't contain in the lstObj, you will get error "System.ArgumentException: DataGridViewComboBoxCell value is not valid". So please make sure all the value of the cells of that ComboBoxColumn should be the value in the lstObj.
If you have any further question, please feel free to tell me.
Sincerely,
Kira Qian
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the
All-In-One Code Framework!