为什么不是';我的数据网格正在更新数据库吗?WPF.NET 4.5

本文关键字:数据库 更新 WPF NET 我的 网格 数据网 数据 为什么不 | 更新日期: 2024-10-18 12:14:29

我正在尝试将数据库表绑定到数据网格视图。我只想显示某些字段,忽略与用户无关的字段,如ID、CREATED_BY、CREATED_ON等。我可以正确加载列表,但更新回数据库不起作用。

编辑-清理了一些代码。

这就是我必须加载数据网格视图的内容。XAML:

<Grid Margin="0">
    <DataGrid x:Name="ProductsDataGrid" AutoGenerateColumns="False" Style="{StaticResource AzureDataGrid}" Background="White" IsTextSearchEnabled="True" Margin="0,0,0,50" RenderTransformOrigin="0.479,0.06">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Name"  Binding="{Binding FP1_NAME}" Width="Auto"/>
            <DataGridCheckBoxColumn ElementStyle="{DynamicResource MetroDataGridCheckBox}"
                EditingElementStyle="{DynamicResource MetroDataGridCheckBox}"
                Header="Is Needed"
                            />
        </DataGrid.Columns>
    </DataGrid>
    <Button x:Name="ProductsGridUpdate" Content="update" Style="{StaticResource AccentedSquareButtonStyle}" HorizontalAlignment="Left" Margin="424,280,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="1.453,1.103" Click="ProductsGridUpdate_Click"/>
</Grid>

下面是代码:

private void LoadList()
    {
        //init
        ProductsDataGrid.Items.Clear();
        ProductsDataGrid.BeginInit();
        ProductsDataGrid.ItemsSource = SQLCommands.GR1_DataSet.fp1_food_products;
        ProductsDataGrid.DataContext = SQLCommands.GR1_DataSet.fp1_food_products;
        ProductsDataGrid.EndInit();
    }
#endregion
private void ProductsGridUpdate_Click(object sender, RoutedEventArgs e)
{ // this does not work? No error is thrown, and it runs. DB just doesn't update. 
    SQLCommands.fp1_adapter.Update (SQLCommands.GR1_DataSet.fp1_food_products);
}

以下是我在应用程序启动时运行的内容,为加载列表做准备:

public static void SQLFoodProductsProvider()
{
    GR1_DataSet = new GroceryListDataSet();
    fp1_adapter = new GroceryListDataSetTableAdapters.fp1_food_productsTableAdapter();
    fp1_adapter.Fill(GR1_DataSet.fp1_food_products);

    //GR1_DataSet.fp1_food_products.fp1_food_productsRowChanged +=
    //    new GroceryListDataSet.fp1_food_productsRowChangeEventHandler(MainWindow.fp1_food_productsRowModified);
    //GR1_DataSet.fp1_food_products.fp1_food_productsRowDeleted +=
    //    new GroceryListDataSet.fp1_food_productsRowChangeEventHandler(MainWindow.fp1_food_productsRowModified);
}

非常感谢您的帮助或指导。提前谢谢。如果这很重要的话,我使用Visual Studio创建了数据库。

为什么不是';我的数据网格正在更新数据库吗?WPF.NET 4.5

好吧。我已经正式过早地秃顶了,但我已经想好了。在DB对象上,有一个属性"复制到输出目录"。

默认情况下,它被设置为'Always Copy'。将其设置为'Copy if newer'解决了我的所有问题。

我会留下来,因为在所有的StackOverflow帖子(以及其他网站)中,我还没有将其视为一个可能的解决方案。