DataGridComboBoxCell-无法以编程方式填充值 [英] DataGridComboBoxCell - Can't Fill Values Programmatically
问题描述
我要填充一个包含两列的DataGridView,一个用于命名标签的 DataGridViewTextBoxColumn
和一个用于选择要映射的SQL列名称的下拉菜单 DataGridViewComboBoxColumn
名称.
我要遍历我的名字,并为每个名字创建一行.每行应显示一个字段的名称( String
对象),另一列应是一个组合框(值在 List(字符串)
中).我不断引发异常,说值无效":
对于每个sourceColumn作为sourceColumnList中的字符串昏暗的项目作为新的DataGridViewTextBoxCell昏暗的destcombo作为新的DataGridViewComboBoxCellitem.Value = sourceColumndestcombo.Items.AddRange(sqlColumnSelectionList.ToArray())'这里是错误,是指无效的组合框|'\ |/list_Destination_SQLMap.Rows.Add(sourceColumn,destcombo)下一个
我已经四处搜寻,但只能找到有关如何使用绑定的而非未绑定的数据源解决此问题的示例.我知道我缺少什么,这是我第一次使用 DataGridView
.
添加 DataGridViewComboBoxColumn
时,还需要将其 DataSource
属性设置为有效源. DataTable
,如果您从数据库或 List(Of T)
中获取数据-您的 List(Of String)
可以正常工作./p>
在创建并填充 sqlColumnSelectionList
之后,在下面添加代码.(不要忘记将变量名称 column2
更改为您命名的名称)
column2.DataSource = sqlColumnSelectionList
重要的是要记住,在初始化和一般使用时, DataGridViewComboBox
的工作方式与普通的 ComboBox
一样.
完成此操作后,在添加行时,应更改以下行:
list_Destination_SQLMap.Rows.Add(sourceColumn,destcombo)
成为:
list_Destination_SQLMap.Rows.Add(sourceColumn,")
这会将新行添加到DataGridView中,在文本"列中有一个与字段名称匹配的条目(根据您的描述),第二列为空白,允许用户从下拉列表中选择值.
如果需要将第二列预选为一个值,请用该值替换空字符串.
DataGridViewComboBoxColumn只需要填充一次,而不是每次将新单元格添加到DataGridView时都填充.
I'm filling a DataGridView with 2 columns, a DataGridViewTextBoxColumn
for a name label, and a DataGridViewComboBoxColumn
to act as a dropdown for selecting an SQL Column name to map to the name.
I'm going for a loop through my names, and creating a row for each of them. Each row should display the name of a field (String
object), and in the other column should be a combo box (values are within a List (Of String)
). I keep getting an exception raised that says 'the value is not valid':
For Each sourceColumn As String In sourceColumnList
Dim item As New DataGridViewTextBoxCell
Dim destcombo As New DataGridViewComboBoxCell
item.Value = sourceColumn
destcombo.Items.AddRange(sqlColumnSelectionList.ToArray())
' ERROR IS HERE, REFERRING TO INVALID COMBO BOX |
' \|/
list_Destination_SQLMap.Rows.Add(sourceColumn, destcombo)
Next
I've googled around but can only find examples of how to fix this problem with data sources that are bound, not unbound. I know I'm missing something, this is the first time I've used a DataGridView
.
When adding a DataGridViewComboBoxColumn
you also need to set its DataSource
property to a valid source. A DataTable
if you get the data from a database or List (Of T)
- Your List (Of String)
will work just fine.
Add the code below AFTER you have created and populated the sqlColumnSelectionList
. (Don't forget to change the variable name column2
to whatever you named it)
column2.DataSource = sqlColumnSelectionList
It is important to remember that the DataGridViewComboBox
works just like a normal ComboBox
when it comes to initialisation and general usage.
Once you have that done, when adding rows you should change this line:
list_Destination_SQLMap.Rows.Add(sourceColumn, destcombo)
to be:
list_Destination_SQLMap.Rows.Add(sourceColumn, "")
This will add the new row to the DataGridView with an entry in the Text column matching the name of the field (as per your description) and the second column will be blank allowing the user to select the value from the drop down.
If you need the second column to be pre-selected to a value, replace the empty string with that value.
The DataGridViewComboBoxColumn only needs to be populated the once, and not every time a new cell is added to the DataGridView.
这篇关于DataGridComboBoxCell-无法以编程方式填充值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!