且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将值分配给datagridview中的组合框列

更新时间:2022-02-14 23:21:24

如果 BindingSource 不起作用,请尝试这样:

If the BindingSource does not work, try like this:
AliasDataTypeCombo.Items.AddRange("One", "Two", "Three", "Four");

也可能是范围问题您直接分配 BindingSource ,尝试使用公共BindingSource变量。

Could also be a scope problem as you assign the BindingSource directly, try with a public BindingSource variable.


如果在执行SetDefaultType()方法后组合框是空白的那么最可能的原因是不正确或没有价值设计到每个组合框。问题变成怎么会发生这种情况?



你从Items集合中获取默认值的代码似乎是正确的所以我有这些简单的问题(不要冒犯了)



1)这个循环条件是否正确,因为它排除了DGV中的最后一行?

If the comboboxes are blank after the SetDefaultType() method has executed then the most likely reason is that either an incorrect or no value was assigned to each combobox. The question becomes how could that happen?

Your code for getting the default values from the Items collection seems to be correct so I have these simple questions (don't be offended)

1) Is this loop condition correct as it excludes the last row in the DGV?
for (int i = 0; i < dgvSchemaMapped.Rows.Count - 1; i++)



2)您使用的是DataGridView.DataError事件吗?在调试期间这几乎是必不可少的,因为DGV具有吞咽异常的恶习,并且事件通常是揭示数据问题的唯一方式。例如


2) Are you using the DataGridView.DataError event. It is almost essential during debugging as the DGV has a nasty habit of swallowing exceptions and the event is often the only way to reveal a problem with the data. For example

comboboxCell.Value = SomethingNotInTheItemsList;

是一个数据错误,会给出一个空白单元格。



3)在switch中,dataType.ToLower()是一个有效值。我问是否有任何开关部分(案例)执行。可能会添加一个带有异常throw语句的默认情况,以突出显示dataType中的意外值。



关于代码的两条评论



复杂的索引/强制转换/索引/强制转换/分配一个班轮真的很难理解,并将其拆分成单独的部分将有助于调试。只有最后两行需要在开关内部。

is a data error and will give a blank cell.

3) In the switch is dataType.ToLower() a valid value. I'm asking if any of the switch sections (cases) execute. Possibly add a default case with an exception throw statement to highlight an unexpected values in dataType.

Two comments about the code

That complex index/cast/index/cast/assign one liner is really difficult to understand and splitting it up into individual parts will aid debugging. Only the last two lines need to be inside the switch.

DataGridViewRow row = grid.Rows[i];

DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)row.Cells[3];
DataGridViewComboBoxCell.ObjectCollection items = cell.Items;

KVP defaultItem = (KVP)items[18];  // KVP is an alias for the full KeyValuePair<>
cell.Value = defaultItem.Value;



为什么作为BindingSource的组合框项目的字典必须隐式转换为IBindingList。



Alan。


Why a Dictionary for the combobox items as the BindingSource has to do an implicit conversion to an IBindingList.

Alan.