拥有datagridview,其中包含在datagridview创建者中手动创建的4列。第1列,第2列和第3列只是文本框列,但最后一列标记为组合框列。在我的表单上我有按钮,当用户点击它时,新行被添加到datagridview。对于前三列,从一些变量中填充数据,并且应该从数据源为用户填充最后一个组合框列,以便他可以从中选择其值。问题是这个组合框列有问题,到目前为止还无法填写。这是我的实际代码
Private Sub myButton_Click(sender As Object, e As EventArgs) Handles btnAddMatType.Click
Dim dt as DataTable
dt = New Variation().GetAll() 'returning Ids and Names
Dim cbo = CType(dgvMaterials.Columns(3), DataGridViewComboBoxColumn)
cbo.Items.AddRange(dt.AsEnumerable().Select(Function(s) s.Field(Of String)("Name")).ToArray())
try
Dim rodzajID as String = TreeMaterials.SelectedValue
Dim rodzajName as string = TreeMaterials.SelectedNode.Text
Dim material as string = TreeMaterials.SelectedNode.Parent.Text
dgvMaterials.Rows.Add(material, rodzajName, rodzajID)
End Sub
也试过这个:
Private Sub myButton_Click(sender As Object, e As EventArgs) Handles btnAddMatType.Click
Dim dt as DataTable
dt = New Variation().GetAll() 'returning Ids and Names
Dim cbo = CType(dgvMaterials.Columns(3), DataGridViewComboBoxColumn)
cbo.DataSource = dt
cbo.ValueMember = "Id"
cbo.DisplayMember = "Name"
Dim rodzajID as String = TreeMaterials.SelectedValue
Dim rodzajName as string = TreeMaterials.SelectedNode.Text
Dim material as string = TreeMaterials.SelectedNode.Parent.Text
dgvMaterials.Rows.Add(material, rodzajName, rodzajID)
End Sub
在这两种情况下,每次用户点击按钮行都会添加到datagrid但最后一列的组合框为空。怎么解决?
答案 0 :(得分:0)
尝试添加此行以将您的comboBox添加到所需列:
dgvMaterials.Columns.Insert(3, cbo)