two way wpf datagrid binding to database

℡╲_俬逩灬. 提交于 2019-12-06 08:39:39

问题


HI all,

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>

UPDATED

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

 public Window1()
    {
        InitializeComponent();

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

    }

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....


回答1:


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.



来源:https://stackoverflow.com/questions/3084831/two-way-wpf-datagrid-binding-to-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!