双向wpf datagrid绑定到数据库 [英] two way wpf datagrid binding to database

查看:514
本文介绍了双向wpf datagrid绑定到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI all,

我想以两种方式绑定WPF数据网格。我尝试过以下XAML:

I want to bind WPF datagrid in two way. I had tried following XAML:

<Grid>
    <my:DataGrid x:Name="dataGrid"  AutoGenerateColumns="False" Margin="8">
        <my:DataGrid.Columns>
            <my:DataGridTextColumn Header="Header" Binding="{Binding pCode}" IsReadOnly="True" />
            <my:DataGridTextColumn Header="Header" Binding="{Binding pName}" IsReadOnly="True" />
            <my:DataGridTextColumn Header="Header" Binding="{Binding pStock}" IsReadOnly="True" />
            <my:DataGridTextColumn Header="Header" Binding="{Binding pGroup}" IsReadOnly="True" />
            <my:DataGridTextColumn Header="Header" Binding="{Binding pPrice, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />              
        </my:DataGrid.Columns>
    </my:DataGrid>
</Grid>

更新

ProductsTableAdapters.TempTA tempTA = new WpfDataGridBinding.ProductsTableAdapters.TempTA();
Products.TempDataTable tempDT = new Products.TempDataTable();

 public Window1()
    {
        InitializeComponent();

        tempDT = tempTA.GetData();
        dataGrid.ItemsSource = tempDT;

    }

这是我如何绑定到datagrid。现在我要更新DB,只要我更改DataGrid中提交的价格。我更想要的是,我只会更新其值已更改的行,而不是所有行。

This is how I am binding to datagrid. Now I want to update DB whenever I change price filed in DataGrid. I more thing I would like to ask that i would update only row whose value has changed, not all rows.

感谢
请代码(帮助)我。 ...

Thanks Please code(help) me....

推荐答案

您不需要明确设置双向绑定。这是默认值。因为您绑定到TableAdapter,所以每当在Grid中进行更改时,这些更改将反映在TableAdapter中。您现在所需要做的是将这些更改写入数据库。在TableAdapter的RowChanged事件中,只需调用TableAdapter.Update将更改写入数据库。

You don't need to explicitly set two way binding. It's the default. Because you're binding to the TableAdapter, whenever you make changes in the grid those changes will be reflected in the TableAdapter. All you need to do now is write those changes to the database. In your RowChanged event of the TableAdapter just call TableAdapter.Update to write the changes to the database.

这篇关于双向wpf datagrid绑定到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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