在绑定的Datagridview中添加和填充DataGridViewComboBoxcolumn [英] Adding and Populating DataGridViewComboBoxcolumn to Bound Datagridview
问题描述
我正在尝试允许DataGridView允许用户编辑字段(为简单起见,假设它只是Item,每个Item都可以有一个Condition,其允许值取自第二个表[Conditions]
I'm attempting to allow a DataGridView to allow the user to edit fields (for simplicity's sake, let's say it's just Items, each of which can have a Condition, with allowable values taken from a second table [Conditions]
例如
- ID(主键-不显示)
- ItemNum
- 数量
- ConditionAbbrev(例如新,已使用,重新认证)-用作下表条件表中某项的键
- ID (Primary key--not to be shown)
- ItemNum
- Qty
- ConditionAbbrev (e.g. "New", "Used", "Recert") -- used as a key to an item in the Conditions table below
- ConditionAbbrev(例如新建,已使用,重新认证等。
- ConditionDescription(条件的详细说明)
我的网格应如下所示:
Item # Qty Condition
123456 10 [ New v] <-- A drop-down
234567 55 [ Used v]
345678 99 [ New v]
etc.
策略:
我尝试通过以下方式进行设置:
The Strategy:
I'm attempting to set this up by:
-
将网格绑定到第一个项目表(获取包含项目
表每一行的实际值的前三个
列)
Binding the grid to the first Items table (grabbing the first three columns which contain the actual values of each line of the Items table)
创建一个新的DataGridViewComboBoxColumn ( CondCombo)并将
的条件表中的所有允许项都绑定到它,
Creating a new DataGridViewComboBoxColumn ("CondCombo") and binding all allowable items from the Conditions table to it,
在网格中循环并为以下项设置CondCombo的值:每个
行到条件行的值
Looping through the grid and setting the value of CondCombo for each row to the value of the Conditions row
隐藏条件(文本)列。
问题:
我能够添加列并加载条件值,但是我完全无法将所选值设置为与Items表中的Condition相匹配;此外,当我按Tab键或单击另一个单元格时,我在组合中所做的所有选择都会被遮住。
The Problem:
I'm able to get the column added and loaded with Conditions values, but I'm utterly failing at setting the selected value to match the Condition from the Items table; furthermore, any selections I'm making in a combo is being blanked the moment I tab or click to another cell.
这是到目前为止我得到的代码:任何
Here's the code I've got so far: any help would be HUGELY appreciated!
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
推荐答案
您正在设置 DataPropertyName
错误。您将一个列表绑定到网格,将一个列表绑定到列。 DisplayMember
和 ValueMember
是列名/绑定到该列的项目的属性。 DataPropertyName
是绑定到网格的项目的列/属性的名称。就您而言,在我看来 DataPropertyName
应该设置为 Condition。
You're setting the DataPropertyName
incorrectly. You bind one list to the grid and one to the column. The DisplayMember
and ValueMember
are names of columns/properties of the items bound to the column. The DataPropertyName
is the name of a column/property of the items bound to the grid. In your case, it looks to me like DataPropertyName
should be set to "Condition".
这篇关于在绑定的Datagridview中添加和填充DataGridViewComboBoxcolumn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!