Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用实体框架刷新WPF中的数据网格_C#_Wpf_Entity Framework_Datagrid - Fatal编程技术网

C# 如何使用实体框架刷新WPF中的数据网格

C# 如何使用实体框架刷新WPF中的数据网格,c#,wpf,entity-framework,datagrid,C#,Wpf,Entity Framework,Datagrid,我是编程初学者。我在C#中开发了一个WPF应用程序,并使用实体框架和DevXPress组件。我有一个GridControl组件dgv_SupportComponent。我想在单击btn\u Void时刷新dgv\u SupportComponent XAML标记是: <Window.Resources> <dxcore:EntityCollectionViewSource x:Key="EntityCollectionViewSource" Culture="en-US

我是编程初学者。我在C#中开发了一个WPF应用程序,并使用实体框架和DevXPress组件。我有一个GridControl组件
dgv_SupportComponent
。我想在单击
btn\u Void
时刷新
dgv\u SupportComponent

XAML标记是:

<Window.Resources>
    <dxcore:EntityCollectionViewSource x:Key="EntityCollectionViewSource" Culture="en-US" ContextType="{x:Type HadishDataModelLayer:HadishDataBaseEntities}" CollectionViewType="{x:Type CollectionView}" Path="vw_SupportComponent">
        <dxcore:DesignDataManager.DesignData>
            <dxcore:DesignDataSettings RowCount="5"/>
        </dxcore:DesignDataManager.DesignData>
    </dxcore:EntityCollectionViewSource>
</Window.Resources>
<Grid Margin="0,0,-0.4,0" >
    <dxg:GridControl x:Name="dgv_SupportComponent" AutoGenerateColumns="None" EnableSmartColumnsGeneration="True" Margin="0,-1,0.4,0.4" SelectionMode="Cell" AllowLiveDataShaping="True" ItemsSource="{Binding Data, Source={StaticResource EntityCollectionViewSource} , UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True }" >
        <dxg:GridControl.View>
            <dxg:TableView x:Name="tlv_support"  ShowTotalSummary="True" AllowEditing="True" AllowPerPixelScrolling="True" RowUpdated="tlv_support_RowUpdated" EditFormPostMode="Immediate" AllowCascadeUpdate="True" AllowGroupSummaryCascadeUpdate="True" ShowAutoFilterRow="True" ShowSearchPanelFindButton="True" ShowSearchPanelMRUButton="True" ShowSearchPanelNavigationButtons="True" SearchPanelAllowFilter="True" SearchColumns="ComponentSt" ShowSearchPanelMode="Never" SearchString="Active" SearchPanelHighlightResults="False" NavigationStyle="Cell" ShowGroupFooters="True" EnableImmediatePosting="True" ShowCriteriaInAutoFilterRow="True" ShowCheckBoxSelectorColumn="True" NewItemRowPosition="Top" IsSynchronizedWithCurrentItem="True" />
        </dxg:GridControl.View>
        <dxg:GridColumn x:Name="CulComponentID" FieldName="ComponentID" IsSmart="True" AllowEditing="True" />
        <dxg:GridColumn FieldName="ComponentName" IsSmart="True" AllowEditing="True" FilterPopupMode="CheckedList" />
        <dxg:GridColumn FieldName="ComponentWeight" IsSmart="True" AllowEditing="True" />
        <dxg:GridColumn FieldName="ComponentWarehouseName" IsSmart="True" />
        <dxg:GridColumn FieldName="ApprovedBy" IsSmart="True"/>
        <dxg:GridColumn FieldName="ComponentWaste" />
        <dxg:GridColumn FieldName="ComponentSt" />
    </dxg:GridControl>
</Grid>

要刷新绑定到EntityCollectionViewSource的GridControl,需要创建EntityCollectionViewSource的新实例。请查看讨论此主题的支持票证。

要刷新绑定到EntityCollectionViewSource的GridControl,需要创建EntityCollectionViewSource的新实例。请查看讨论此主题的支持票证。

如果您在这里尝试一些MVVM会怎么样

您的网格

<ListView Name="MyListView" ItemsSource="{Binding AllItems}">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Id" DisplayMemberBinding="{Binding Id}"/>
            <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/>
            <GridViewColumn Header="Approuved By" DisplayMemberBinding="{Binding Approver}"/>
        </GridView>
    </ListView.View>
</ListView>
UpdateSelected
实现:

private void UpdateSelected(object selected)
{
    var selectedItem = selected as Item;
    if (selectedItem != null)
    {
        var item = AllItems.SingleOrDefault(i => i.Id == selectedItem.Id);
        if (item != null)
        {
            item.Approver = "New Approver";
        }
    }
}

如果您在这里尝试一些MVVM会怎么样

您的网格

<ListView Name="MyListView" ItemsSource="{Binding AllItems}">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Id" DisplayMemberBinding="{Binding Id}"/>
            <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/>
            <GridViewColumn Header="Approuved By" DisplayMemberBinding="{Binding Approver}"/>
        </GridView>
    </ListView.View>
</ListView>
UpdateSelected
实现:

private void UpdateSelected(object selected)
{
    var selectedItem = selected as Item;
    if (selectedItem != null)
    {
        var item = AllItems.SingleOrDefault(i => i.Id == selectedItem.Id);
        if (item != null)
        {
            item.Approver = "New Approver";
        }
    }
}

<Button Command="{Binding UpdateSelectedCommand}" CommandParameter="{Binding ElementName=MyListView, Path=SelectedItem}" Content="Refresh Selected"  Margin="10" Width="100">
    <Button.Style>
        <Style>
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=MyListView, Path=SelectedItems.Count}" Value="0">
                    <Setter Property="Button.IsEnabled" Value="False"></Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>
UpdateSelectedCommand = new RelayCommand(selected => UpdateSelected(selected));
private void UpdateSelected(object selected)
{
    var selectedItem = selected as Item;
    if (selectedItem != null)
    {
        var item = AllItems.SingleOrDefault(i => i.Id == selectedItem.Id);
        if (item != null)
        {
            item.Approver = "New Approver";
        }
    }
}