两种方式将 wpf datagrid 绑定到数据库
大家好,
我想以两种方式绑定 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要显式设置双向绑定。这是默认设置。因为您绑定到 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.