【问题标题】:Add combobox to datagrid that is linked to a datable将组合框添加到链接到数据表的数据网格
【发布时间】:2013-01-25 05:37:56
【问题描述】:

我的程序是一个用 VB.net 编写的 WPF 应用程序。我也接受面向 C# 的答案,因为我应该能够理解和/或转换。

我有一个数据表,我通过 MySqlDataAdapter 填充了我的 MySQL 数据库中的数据。

我的 dataGrid 目前有 AutoGenerateColumns="TRUE"。

我通过DataGrid1.ItemsSource = MyDataTable.DefaultView将我的dataTable数据加载到我的dataGrid中

在我的表中,我有一个标有“callType”的列,我希望它是一个组合框 (DataGridComboBoxColumn)

我尝试了各种可以说是在黑暗中丢失的镜头。

是否有人能够将我推向正确的方向或向我展示一些有关如何在与该 dataGrid 的数据链接的 dataGrid 中创建组合框列的代码?

【问题讨论】:

    标签: mysql vb.net datagrid datatable wpfdatagrid


    【解决方案1】:

    检查How to: Customize Auto-Generated Columns in the DataGrid Control?。它说:

    您可以处理 AutoGeneratingColumn 事件来修改、替换或 取消正在生成的列。自动生成列 事件针对每个公共的、非静态的属性发生一次 更改 ItemsSource 属性时绑定的数据类型和 AutoGenerateColumns 属性为 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
    

    【讨论】:

    • 太棒了!我能够得到一个带有我想要的下拉值的组合框列。 . .但是如何将组合框的值与该行的该列的值连接起来?
    猜你喜欢
    • 2014-05-09
    • 2016-11-11
    • 1970-01-01
    • 2019-11-02
    • 1970-01-01
    • 2021-12-24
    • 2012-09-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多