DataGridViewComboBoxColumn - how do I set the SelectedItem?

I've added a ComboBox column to my DataGridView, and I'm having trouble specifying, at run time, thet value that is to appear in the combo box, as being selected.
I don't need to bind the fields to a data source, I want them to contain two distinct items. I'm able to add them using "column.Items.Add("xxx")", but when running the app, the combo box is empty, with my added values in the drop down. What I need is something like "column.SelectedIndex = 0" so I can specify the default seleted item.
How can I accomplish this?
Thanks,
John
[562 byte] By [Haashole] at [2007-12-24]
# 1


dataGridViewComboBoxColumn1.Items.Add("abc");
dataGridViewComboBoxColumn1.Items.Add(
"def");

for
(int index = 0; index < dataGridView1.RowCount; index++)
{
DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)dataGridView1.Rows[index].Cells[0];

cell.Value = "abc";
}

Cheers!

dkocur2 at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 2

I have a similar situation where I have integers saved in the database, but would like to display the values in a dropdown. To avoid hardcoding dropdown values I created a enum like this:

private enum FlagDescriptions

{

Ignore = 1,

Critical

}

and in the RowPrePaint event I have something like this:

string WindowFlag = rw.Cells["Flag"].Value.ToString().Trim();

rw.Cells["FlagDescription"].Value = ((FlagDescriptions)int.Parse(WindowFlag)).ToString();

Not ideal but it works, if anyone has a better idea please let me know.

AdriaanDavel at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 3

If I try doing this for four different grids already created and populated, it only seems to work for 2 of the 4. I reference the column by column name to ensure I'm hitting the right one.

While debugging through the code, I see the value properties are being populated properly for all 4 grids.

By the time the gui is visible, 2 of the grids have empty combo boxes.

It seems to happen forthe same 2 grids every time.

I imagine there must something wrong with how I'm creating the grid, since I'm creating the DataGridViewComboBoxColumn exactly the same for each grid.

Any ideas?

rainwhenidie at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 4
I am having the same problem under different circumstances. Perhaps I can rephrase to bring us to a common understanding, and a proper solution:

For a normal combobox control, there are properties DisplayMember and ValueMember, and - the key factor - the SelectedValue property. Read up on the DGVCombobox class. When these are set, the combobox works just fine.

The DGV Combobox has the DisplayMember, and the ValueMember properties, but NOT the SelectedValue property. Its exactly because this property is missing that makes the combobox empty, despite having the proper items in the dropdown list.

So the question is, how do we make this work - what is the equivalent to the SelectedValue Property? Or perhaps another property is set to some value that causes the blank combobox cell? It would be very helpful to know the answer to this question because it would avoid code that sets some value in that DGVcombobox cell. It is supposed to be automatic.

LouArnold at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 5

Not quite.

I have it working for 2 grids.

I created the columns with the following code:

private DataGridViewComboBoxColumn CreateDataGridViewComboBoxColumnForFormats()

{

DataGridViewComboBoxColumn ComboBoxColumn = new DataGridViewComboBoxColumn();

ComboBoxColumn.DataPropertyName = IndexQuotesColumns.Format;

ComboBoxColumn.HeaderText = IndexQuotesColumns.Format;

ComboBoxColumn.Name = IndexQuotesColumns.Format;

ComboBoxColumn.MaxDropDownItems = 2;

ComboBoxColumn.FlatStyle = FlatStyle.System;

ComboBoxColumn.Items.Add("p");

ComboBoxColumn.Items.Add("b");

ComboBoxColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;

ComboBoxColumn.AutoComplete = true;

ComboBoxColumn.DisplayIndex = 2;

return ComboBoxColumn;

}

then I add the column to the grid.

If the column code doesn't change, I figure there must be something wrong with the other 2 grids....

rainwhenidie at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 6

As much as I hated to do it, it worked

Thanks

rainwhenidie at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...