我的程序是一个用 VB.net 编写的 WPF 应用程序。我也接受面向 C# 的答案,因为我应该能够理解和/或转换。
我有一个数据表,我通过 MySqlDataAdapter 从我的 MySQL 数据库中填充数据。
我的 dataGrid 当前有 AutoGenerateColumns="TRUE"。
我通过 DataGrid1.ItemsSource = MyDataTable.DefaultView
将我的 dataTable 数据加载到我的 dataGrid 中
在我的表中,我有一个标记为“callType”的列,我希望它是一个组合框 (DataGridComboBoxColumn)
我已经尝试了各种可以被描述为在黑暗中丢失的镜头。
是否有人能够在正确的方向上插入我或向我展示一些代码,说明如何在与该 dataGrid 的数据链接的 dataGrid 中创建一个组合框列?
最佳答案
检查 How to: Customize Auto-Generated Columns in the DataGrid Control? .它说:
You can handle the AutoGeneratingColumn event to modify, replace, or cancel a column that is being generated. The AutoGeneratingColumn event occurs one time for each public, non-static property in the bound data type when the ItemsSource property is changed and the AutoGenerateColumns property is true.
Xaml:
<DataGrid AutoGeneratingColumn="DataGrid_AutoGeneratingColumn" />
代码:
Private Sub DataGrid_AutoGeneratingColumn(sender As System.Object, e As System.Windows.Controls.DataGridAutoGeneratingColumnEventArgs)
If e.PropertyName = "callType" Then
Dim combo As New DataGridComboBoxColumn
combo.ItemsSource = MyItemsSource
combo.DisplayMemberPath = "TypeName"
combo.SelectedValuePath = "TypeID"
Dim comboBinding As New Binding("callType")
combo.SelectedValueBinding = comboBinding
e.Column = combo
End If
End Sub
关于mysql - 将组合框添加到链接到数据表的数据网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14789778/