两种方式将 wpf datagrid 绑定到数据库

发布于 2024-09-06 18:20:46 字数 1312 浏览 5 评论 0原文

大家好,

我想以两种方式绑定 WPF 数据网格。我尝试过遵循 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 中提交的价格时,我都想更新数据库。我更想问的是,我只会更新值已更改的行,而不是所有行。

谢谢 请编码(帮助)我......

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不知所踪 2024-09-13 18:20:46

您不需要显式设置双向绑定。这是默认设置。因为您绑定到 TableAdapter,所以每当您在网格中进行更改时,这些更改都会反映在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文