我正在尝试允许DataGridView允许用户编辑字段(为简单起见,假设它只是Items,每个都可以有一个Condition,从第二个表中获取允许的值[条件]
e.g。
我的网格应该如下:
Item # Qty Condition
123456 10 [ New v] <-- A drop-down
234567 55 [ Used v]
345678 99 [ New v]
etc.
我试图通过以下方式进行设置:
将网格绑定到第一个Items表(抓住前三个 包含Items的每一行的实际值的列 表)
创建新的DataGridViewComboBoxColumn(“CondCombo”)和绑定 条件表中的所有允许项目,
循环播放网格并为每个网格设置CondCombo的值 行到条件行的值
隐藏条件(文本)列。
我能够添加和添加条件值列,但是我完全没有设置所选值以匹配Items表中的Condition;此外,当我选中或点击另一个单元格时,我在组合中所做的任何选择都会被删除。
这是我到目前为止的代码:任何帮助都会非常感激!
Sub SetupGrid(byref myGrid as DataGridView,
myConn as sqlite.sqliteConnection)
Dim myAdapter As System.Data.SQLite.SQLiteDataAdapter
myGrid.VirtualMode = true
myAdapter = new system.data.sqlite.sqliteadapter(_
"Select ID, ItemNum, Qty, Condition FROM Items", myConn)
myAdapter.SelectCommand.CommandType = CommandType.Text
' Fill the main grid with the item data
dim ds as new DataSet
myAdapter.Fill(ds)
myGrid.DataSource = ds.Tables(0)
' Now create and load the ComboBox column
dim cboColumn as new DataGridViewComboBoxColumn
With cboColumn
.DataPropertyName = "ConditionAbbrev"
.name = "CondCombo"
.HeaderText = "Cond"
' Bind the ComboColumn
Dim conditionsAdapter As System.Data.SQLite.SQLiteDataAdapter
Dim condTable As DataTable
using cmd as new Sqlite.sqliteCommand(_
"SELECT ConditionAbbrev FROM Conditions", myconn)
conditionsAdapter.selectCommand = cmd
conditionsApapter.fill(condTable)
end using
.DataSource = condTable
.DataPropertyName = "ConditionAbbrev"
.ValueMember = "ConditionAbbrev"
.DisplayMember = .ValueMember
end with
' Set the selected combo member to be the same as the Condition (text) field value:
for each curRow as dataGridViewrow in myGrid.Rows()
curRow.cells("CondCombo").value = _
curRow.Cells("Condition").value
next
' Hide the Condition (text) field)
myGrid.Columns("Condition").visible = false
' Hide the ID field
myGrid.Columns("ID").visible = false
end sub
答案 0 :(得分:0)
您错误地设置了DataPropertyName
。将一个列表绑定到网格,将一个列表绑定到列。 DisplayMember
和ValueMember
是绑定到列的项的列/属性的名称。 DataPropertyName
是绑定到网格的项的列/属性的名称。在您的情况下,我认为应将DataPropertyName
设置为“条件”。