我正在努力解决DataGridViewComboBoxCell
.在某些情况下(比如说,事件(,我必须在表单的DataGridView
中预先选择一个值 ComboBox
。当用户更改一个框时,我可以像这样以编程方式更改另一个框:
var item = ItemName.Items.GetListItem(name);
if (item != null)
{
_loading = true; // that's needed to come around another CellValueChanged events
itemView.Rows[e.RowIndex].Cells["ItemName"].Value = item;
_loading = false;
}
我像这样填充ItemName.Items
:
foreach (var item in _model.DefaultData.ItemList)
{
if (item.Value.Code.HasValue()) ItemCode.Items.Add(new ListItem(item.Key, item.Value.Code));
ItemName.Items.Add(new ListItem(item.Key, item.Value.Name));
}
获取列表项方法:
public static ListItem GetListItem(this DataGridViewComboBoxCell.ObjectCollection col, string name)
{
ListItem retItem = null;
foreach (ListItem item in col)
{
if (item.Name == name) { retItem = item; break; }
}
return retItem;
}
这工作正常,但是...
现在我想在表单加载时向DataGridView
添加行,如下所示:
foreach (var item in _model.SelectedItems)
{
var row = new DataGridViewRow();
row.Cells.Add(new DataGridViewTextBoxCell { Value = item.Id });
row.Cells.Add(new DataGridViewComboBoxCell { Value = ItemCode.Items.GetListItem(item.Code) });
row.Cells.Add(new DataGridViewComboBoxCell { Value = ItemName.Items.GetListItem(item.Name) });
row.Cells.Add(new DataGridViewTextBoxCell { Value = item.Units });
...
itemView.Rows.Add(row);
}
这抛出了心爱的DataGridViewComboBox value is not valid
例外。请帮忙,我对此没有想法。我不想使用DataSource
或类似的东西。 ItemCode
和ItemName
列项通过 GetListItem()
正确填充和返回。我不明白为什么它正常工作,但在表单加载时它不起作用(显示它也不起作用(。
编辑:对不起,忘了添加。
我的列表项类:
public class ListItem
{
public int Id { get; set; }
public string Name { get; set; }
public ListItem(int sid, string sname)
{
Id = sid;
Name = sname;
}
public override string ToString()
{
return Name;
}
}
我已经把它放在表单加载上了:
ItemName.ValueMember = "Id";
ItemName.DisplayMember = "Name";
ItemCode.ValueMember = "Id";
ItemCode.DisplayMember = "Name";
好的,我设法自己解决了。
显然,DataGridViewComboBoxColumn.Items
包含可能的项目是不够的。如果要以编程方式添加新行,还必须向DataGridViewComboBoxCell.Items
添加项。
因此,如果其他人尝试使用我没有数据绑定(如DataTable
等(的方法,这是我的解决方案:
foreach (var item in _model.SelectedItems)
{
var row = new DataGridViewRow();
var codeCell = new DataGridViewComboBoxCell();
codeCell.Items.AddRange(ItemCode.Items);
codeCell.Value = ItemCode.Items.GetListItem(item.Code);
var nameCell = new DataGridViewComboBoxCell();
nameCell.Items.AddRange(ItemName.Items);
nameCell.Value = ItemName.Items.GetListItem(item.Name);
row.Cells.Add(new DataGridViewTextBoxCell { Value = item.Id });
row.Cells.Add(codeCell);
row.Cells.Add(nameCell);
row.Cells.Add(new DataGridViewTextBoxCell { Value = item.Units });
row.Cells.Add(new DataGridViewTextBoxCell { Value = item.Quantity });
row.Cells.Add(new DataGridViewTextBoxCell { Value = item.PriceLt });
row.Cells.Add(new DataGridViewTextBoxCell { Value = item.PriceEu });
itemView.Rows.Add(row);
}
DataGridViewColumn[] dgCol =
{
new DataGridViewTextBoxColumn()
{
HeaderText = "Conviction",
SortMode = DataGridViewColumnSortMode.NotSortable,
Width = Convert.ToInt32(0.25f * grdConvictions.Width - 19)
},
new DataGridViewTextBoxColumn()
{
HeaderText = "Points" ,
SortMode = DataGridViewColumnSortMode.NotSortable,
Width = Convert.ToInt32(0.125f * grdConvictions.Width - 19)
},
new DataGridViewTextBoxColumn()
{
HeaderText = "Date",
SortMode = DataGridViewColumnSortMode.NotSortable,
Width = Convert.ToInt32(0.125f * grdConvictions.Width - 19),
},
new DataGridViewComboBoxColumn ()
{
HeaderText = "Galletas",
Width = Convert.ToInt32(0.25 * grdConvictions.Width - 19),
DataPropertyName = "Galletas",
DataSource = new List<String>{"",
"Maiz",
"Pera",
"Manzana",
"Sandia",
"Fresa",
"Melon",
"Melocoton",
"Maracuya",
"Cereza",
"Frambuesa",
"Mora",
"Kiwi",
"Anona",
"Guayaba"
},
SortMode = DataGridViewColumnSortMode.NotSortable,
MaxDropDownItems = 10,
DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing,
ReadOnly = false
}
};
YourDataGridView.Columns.AddRange(dgCol(;
注意:DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox 以查看组合。我使用为"无",并且...:
private void grdConvictions_CellEnter(object sender, DataGridViewCellEventArgs e)
{
int colIndex = e.ColumnIndex;
int rowIndex = e.RowIndex;
if (rowIndex < 0 || colIndex < 0)
{
return;
}
DataGridView grd = (DataGridView)sender;
if (grd[e.ColumnIndex, e.RowIndex].GetType().Name != "DataGridViewComboBoxCell")
{
((DataGridViewComboBoxCell)grd.CurrentCell).DisplayStyle = DataGridViewComboBoxDisplayStyle.DropDownButton;
}
}
private void grdConvictions_CellLeave(object sender, DataGridViewCellEventArgs e)
{
int colIndex = e.ColumnIndex;
int rowIndex = e.RowIndex;
if (rowIndex < 0 || colIndex < 0)
{
return;
}
DataGridView grd = (DataGridView)sender;
if (grd[e.ColumnIndex, e.RowIndex].GetType().Name != "DataGridViewComboBoxCell")
{
((DataGridViewComboBoxCell)grd.CurrentCell).DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
}
}