Setting value for unbound DataGridViewComboBoxCell

J

John

Hi,

I have set a DataGridViewComboBoxColumn for a complex object type, and set
the DisplayMember and ValueMember to members of the object type. My problem
is how to programmatically set the cell value. The objects live in the
DataGridViewComboBoxColumn.Items, but there is no way of getting an object
out based on, e.g. display value.

Here's the type :

private class complexObj
{
public string id_string;
public int id;

public complexObj(string Id_string, int Id)
{
id_string = Id_string;
id = Id;
}

public String ShowVal
{
get { return id_string; }
set { id_string = value; }
}

public complexObj Self
{
get { return this; }
}
}


here's the code that fills the pulldown list and attempts to fill the
grid...

private void Form1_Load(object sender, EventArgs e)
{
(grid.Columns["Column2"] as DataGridViewComboBoxColumn).ValueMember =
"Self";
(grid.Columns["Column2"] as DataGridViewComboBoxColumn).DisplayMember
= "ShowVal";

complexObj mi = new complexObj("One", 1);
(grid.Columns["Column2"] as DataGridViewComboBoxColumn).Items.Add(mi);
mi = new complexObj("Two", 2);
(grid.Columns["Column2"] as DataGridViewComboBoxColumn).Items.Add(mi);
mi = new complexObj("Three", 3);
(grid.Columns["Column2"] as DataGridViewComboBoxColumn).Items.Add(mi);

int pos = grid.Rows.Add();
grid.Rows[pos].Cells["Column1"].Value = "First line";
grid.Rows[pos].Cells["Column2"].Value = "One"; // doesn't
work, woudl like to assign straight out of

// (grid.Columns["Column2"] as
DataGridViewComboBoxColumn).Items
pos = grid.Rows.Add();
grid.Rows[pos].Cells["Column1"].Value = "Second line";
grid.Rows[pos].Cells["Column2"].Value = "Two"; // doesn't
work

pos = grid.Rows.Add();
grid.Rows[pos].Cells["Column1"].Value = "Third line";
grid.Rows[pos].Cells["Column2"].Value = "Three"; // doesn't
work

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top