I seems that I was a bit unclear in my question.
Say the valid values for C2 was 1, 2, and 3 then I set it like this
grid[columnIndex, rowIndex].Value = "2";
(this does not show the value in the cell)
or like this using the combo box:
combobox.SelectedIndex = 1;
(I don't know how to get the combobox from the cell - the closest I get is
in the
eventhandler for the EditingControlShowing event - ComboBox c = e.Control as
ComboBox)
Hi Paul,
Thank you for your prompt reply!
>Say the valid values for C2 was 1, 2, and 3 then I set it like this
>grid[columnIndex, rowIndex].Value = "2";
>(this does not show the value in the cell)
Could you tell me how you bind the DataGridViewComboBoxColumn?
If you bind the DataGridViewComboBoxColumn to a data source and set the
ValueMember and DisplayMember properties, the DataGriedViewComboBoxCell
should show the text of the corresponding DisplayMember, not value of the
ValueMember.
I will illustrate this with an example. Create a WinForm application
project and add a DataGridView and a Button on the form. Add a
DataGridViewComboBoxColumn into the DataGridView control. In the Form load
event handler, create a DataTable with two columns named ID and Name and
add some rows into this DataTable and bind the DataGridViewComboBoxColumn
in the DataGridView to the DataTable.
In the Button Click event handler, set the current cell's value to a
specified value.
The following is the sample code:
private void Form1_Load(object sender, EventArgs e)
{
this.dataGridView1.RowCount = 3;
DataTable lookup = new DataTable();
DataColumn col = new DataColumn("ID", typeof(Int32));
lookup.Columns.Add(col);
col = new DataColumn("Name", typeof(string));
lookup.Columns.Add(col);
DataRow row = lookup.NewRow();
row[0] = 1;
row[1] = "aa";
lookup.Rows.Add(row);
row = lookup.NewRow();
row[0] = 2;
row[1] = "bb";
lookup.Rows.Add(row);
// bind the DataGridViewComboBoxColumn to the DataTable
(this.dataGridView1.Columns[0] as
DataGridViewComboBoxColumn).DataSource = lookup;
(this.dataGridView1.Columns[0] as
DataGridViewComboBoxColumn).ValueMember = "ID";
(this.dataGridView1.Columns[0] as
DataGridViewComboBoxColumn).DisplayMember = "Name";
}
private void button1_Click(object sender, EventArgs e)
{
this.dataGridView1.CurrentCell.Value = 2; // the current cell's
value becomes 2 and the text displayed in the current cell becomes "bb"
}
> I don't know how to get the combobox from the cell - the closest I get is
in the eventhandler for the EditingControlShowing event - ComboBox c =
e.Control as ComboBox
To get the hosted ComboBox in the DataGridViewComboBoxCell, we usually
handle the EditingControlShowing event of the DataGridView. But I don't
think you need to get the hosted ComboBox in order to get the valid values
for the DataGridViewComboBoxCell.
Hope this helps.
If you have any question, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support
Paul S - 13 Nov 2007 12:42 GMT
Hi Linda
We're getting closer.
I've added the c2 column to your sample and handled the EndEnit event
private void dataGridView1_CellEndEdit(object sender,
DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 1)
{
if (this.dataGridView1.CurrentCell.Value == "1")
this.dataGridView1[0, e.RowIndex].Value = 1;
if (this.dataGridView1.CurrentCell.Value == "2")
this.dataGridView1[0, e.RowIndex].Value = 2;
}
}
This illustrates my problem not being able to set the value in the first
column containing the Combo box.

Signature
Sincerely
Paul S
Linda Liu[MSFT] - 14 Nov 2007 10:09 GMT
Hi Paul,
Thank you for your quick response!
The CellEndEdit event of a DataGridView occurs when edit mode stops for the
current selected cell.
For example, after you type "1" in the first row and second column within
the DataGridView, you should press the Enter key or select another cell to
make the CellEndEdit event raised. Then you should see the text displayed
in the first row and first column is "aa".
If you have any question, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support
Paul S - 14 Nov 2007 11:28 GMT
Hi Linda
I agree on everything you say. When I leave the cell in the second column
the eventhandler for the CellEndEdit gets executed and there I set the value
for the first column containing the combo box.
This is not happening:
Then you should see the text displayed
in the first row and first column is "aa"

Signature
Sincerely
Paul S
Linda Liu[MSFT] - 15 Nov 2007 04:10 GMT
Hi Paul,
Could you please tell me what version of the Visual Studio you're using?
I'm using VS05.
Since I didn't see the same result as you, it would be better for you to
create a simple project that could just reproduce the problem and send it
to me.
To get my actual email address, remove 'online' from my displayed email
address.
Sincerely,
Linda Liu
Microsoft Online Community Support
Paul S - 15 Nov 2007 07:25 GMT
Hi Linda
In the progress of creating a simple sample I saw that what you're saying is
right and what's causing my problems is how I adress the cells
private void dataGridView1_CellEndEdit(object sender,
DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 1)
{
string val = this.dataGridView1.CurrentCell.Value.ToString();
DataGridViewCell cell = this.dataGridView1[e.ColumnIndex,
e.RowIndex];
if (string.Equals(val, "1"))
cell.OwningRow.Cells[e.ColumnIndex].Value = 1;
if (string.Equals(val, "2"))
this.dataGridView1[0, e.RowIndex].Value = 2;
}
}
2) works, but 1 doesn't. I thought that what I did in 1) was just another
way to do the same as in 2).

Signature
Paul S
> Hi Paul,
>
[quoted text clipped - 11 lines]
> Linda Liu
> Microsoft Online Community Support
Linda Liu[MSFT] - 15 Nov 2007 09:55 GMT
Hi Paul,
Thank you for your prompt reply!
The statement 'cell.OwningRow.Cells[e.ColumnIndex]' refers to the cell
itself.
Modify the line of code
cell.OwningRow.Cells[e.ColumnIndex].Value = 1;
to
cell.OwningRow.Cells[0].Value =1;
should solve the problem.
If you have any question, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support
Paul S - 23 Nov 2007 11:50 GMT
Thanks Linda
You're absolytely right - silly mistake on my side - sorry

Signature
Sincerely
Paul S
> Hi Paul,
>
[quoted text clipped - 15 lines]
> Linda Liu
> Microsoft Online Community Support