将combobox添加到链接到datable的datagrid [英] Add combobox to datagrid that is linked to a datable

查看:140
本文介绍了将combobox添加到链接到datable的datagrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我有一个dataTable,我填写了我的MySQL数据库中的数据, MySqlDataAdapter。



我的dataGrid目前有AutoGenerateColumns =TRUE。



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



在我的表中,我有一个标题为callType的列,喜欢成为一个comboBox(DataGridComboBoxColumn)



我已经尝试过各种各样的东西,可以描述为在黑暗中失踪的镜头。

$ b有人能够推动我正确的方向,或者向我展示一些如何在与DataGrid的数据链接的dataGrid中创建一个comboBox列的代码?

解决方案

检查如何:自定义自动格式DataGrid控件中的紧张列?它说:


您可以处理AutoGeneratingColumn事件以修改,替换或
取消正在生成的列。当ItemsSource属性更改并且
AutoGenerateColumns属性为true时,AutoGeneratingColumn
事件会在
绑定数据类型中的每个公共非静态属性发生一次。


Xaml:

 < DataGrid AutoGeneratingColumn =DataGrid_AutoGeneratingColumn /> 

代码:

 code> Private Sub DataGrid_AutoGeneratingColumn(sender As System.Object,e As System.Windows.Controls.DataGridAutoGeneratingColumnEventArgs)

如果e.PropertyName =callType然后

Dim Combo As New DataGridComboBoxColumn

combo.ItemsSource = MyItemsSource
combo.DisplayMemberPath =TypeName
combo.SelectedValuePath =TypeID

Dim comboBinding作为新绑定(callType)
combo.SelectedValueBinding = comboBinding

e.Column =组合

结束如果

结束子


My program is a WPF application written in VB.net. I accept answers geared toward C# as well, as I should be able to understand and/or convert.

I have a dataTable that I fill with data from my MySQL database via MySqlDataAdapter.

My dataGrid currently has AutoGenerateColumns="TRUE".

I load my dataTable data into my dataGrid by DataGrid1.ItemsSource = MyDataTable.DefaultView

In my table I have a column labeled "callType" that I would like to have be a comboBox (DataGridComboBoxColumn)

I have tried a variety of things that can be described as shots in the dark that are missing.

Would someone be able to nudge me in the right direction or show me some code on how to make a comboBox column in a dataGrid that is linked with the data of that dataGrid?

解决方案

Check How to: Customize Auto-Generated Columns in the DataGrid Control? . It says :

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"  />

Code :

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

这篇关于将combobox添加到链接到datable的datagrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆