about DataGridViewComboBoxCell

A

aXqd

i want a editable comboboxcell for a datagridview, i have done these:

[dgvConfigShow is a DataGridView]

public partial class Form1 : Form
{
....
private static List<string> _tempList = new List<string>(new string[]
{});
....
private void lbTypes_SelectedIndexChanged(object sender, EventArgs e)
{
foreach(...)
{
int newRowIndex = dgvConfigShow.Rows.Add();
DataGridViewRow newRow = dgvConfigShow.Rows[newRowIndex];

DataGridViewCell dgvcellCategoryName =
newRow.Cells["dgvcolCategoryName"];
dgvcolCategoryName.DataSource = _tempList;
}
}
....
private void dgvConfigShow_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
DataGridViewComboBoxEditingControl comboControl = e.Control as
DataGridViewComboBoxEditingControl;
if (comboControl != null)
if (comboControl.DropDownStyle != ComboBoxStyle.DropDown)
comboControl.DropDownStyle = ComboBoxStyle.DropDown;
}
....
private void dgvConfigShow_CellValidating(object sender,
DataGridViewCellValidatingEventArgs e)
{
DataGridViewComboBoxCell cell = dgvConfigShow.CurrentCell as
DataGridViewComboBoxCell;
if (cell == null) return;

string curVal = e.FormattedValue as string;
if (!String.IsNullOrEmpty(curVal) && !_tempList.Contains(curVal))
_tempList.Add(curVal);

cell.Value = curVal;
}
....
}

everything goes fine until i try this:

input "a" into row 1 <-> _tempList = ["a"]

input "b" into row 2 <-> _tempList = ["a", "b"]

input "a" into row 1 <-> _tempList = ["a", "b"]

input/select "b" on row 1 <-> _tempList = ["a", "b"] but meanwhile
raised the well-known exception (DataGridViewComboBoxCell: Value is
not valid)

any idea, please?
 
A

aXqd

i want a editable comboboxcell for a datagridview, i have done these:

[dgvConfigShow is a DataGridView]

public partial class Form1 : Form
{
...
private static List<string> _tempList = new List<string>(new string[]
{});
...
private void lbTypes_SelectedIndexChanged(object sender, EventArgs e)
{
foreach(...)
{
int newRowIndex = dgvConfigShow.Rows.Add();
DataGridViewRow newRow = dgvConfigShow.Rows[newRowIndex];

DataGridViewCell dgvcellCategoryName =
newRow.Cells["dgvcolCategoryName"];
dgvcolCategoryName.DataSource = _tempList;}
}

...
private void dgvConfigShow_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
DataGridViewComboBoxEditingControl comboControl = e.Control as
DataGridViewComboBoxEditingControl;
if (comboControl != null)
if (comboControl.DropDownStyle != ComboBoxStyle.DropDown)
comboControl.DropDownStyle = ComboBoxStyle.DropDown;}

...
private void dgvConfigShow_CellValidating(object sender,
DataGridViewCellValidatingEventArgs e)
{
DataGridViewComboBoxCell cell = dgvConfigShow.CurrentCell as
DataGridViewComboBoxCell;
if (cell == null) return;

string curVal = e.FormattedValue as string;
if (!String.IsNullOrEmpty(curVal) && !_tempList.Contains(curVal))
_tempList.Add(curVal);

cell.Value = curVal;

}
...
}

everything goes fine until i try this:

input "a" into row 1 <-> _tempList = ["a"]

input "b" into row 2 <-> _tempList = ["a", "b"]

input "a" into row 1 <-> _tempList = ["a", "b"]

input/select "b" on row 1 <-> _tempList = ["a", "b"] but meanwhile
raised the well-known exception (DataGridViewComboBoxCell: Value is
not valid)

any idea, please?

what i want is the datagridviewcomboboxcells of each row share the
same datasource, and each one of them is able to add items into the
common datasource by typing into/select the combobox.
 

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