2017-06-10 28 views
1

私はプログラミングの初心者です。私はC#でWPFアプリケーションを開発し、Entity FrameworkとDevexpressコンポーネントを使用します。私はGridControlコンポーネントdgv_SupportComponentを持っています。 btn_Voidをクリックすると、dgv_SupportComponentが更新されます。Entity Frameworkを使用してWPFでDataGridをリフレッシュする方法

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> 

私はbtn_Voidをクリックしたときにデータグリッドをリフレッシュしたいです。

このコードはデータをSQLに更新しましたが、DataGridは更新されません。

私のコードは次のとおりです。

private void btn_Void_ItemClick(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e) 
{ 
    foreach (int rowHandle in tlv_support.GetSelectedRowHandles()) 
    { 
     int supid = Convert.ToInt32(dgv_SupportComponent.GetCellValue(rowHandle, "ComponentID")); 
     db.SPSupportComponentState(supid, HadishLogicLayer.HadishCode.gCompanyCode, false, HadishLogicLayer.HadishCode.gUserID); 
    } 

    dgv_SupportComponent.RefreshData(); /// this code dose not refresh datagrid 
} 

答えて

0

ここで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> 

ボタン

<Button Command="{Binding UpdateDataCommand}" Content="Refresh All" Margin="10" Width="100"/> 

のViewModel

public abstract class ViewModelBase : INotifyPropertyChanged 
{ 
    protected void RaisePropertyChanged(string propertyName) 
    { 
     PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
} 


public class Item : ViewModelBase, INotifyPropertyChanged 
{ 
    private string _approver; 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public string Approver 
    { 
     get { return _approver; } 
     set 
     { 
      if (_approver != value) 
      { 
       _approver = value; 
       RaisePropertyChanged(nameof(Approver)); 
      } 
     } 
    } 
} 

public class MyViewModel : ViewModelBase 
{ 

    public MyViewModel() 
    { 
     UpdateDataCommand = new RelayCommand(_ => UpdateAll()); 
    } 

    public ObservableCollection<Item> AllItems { get; set; } = new ObservableCollection<Item>(); 

    public ICommand UpdateDataCommand { get; set; } 
    private void UpdateAll() 
    { 
     //Fetch from DB 
     var items = new[] 
     { 
      new Item {Id=1, Name="Item 1", Approver="Lance"}, 
      new Item {Id=2, Name="Item 2", Approver="John"} 
     }; 

     AllItems.Clear(); 
     foreach (var item in items) 
     { 
      AllItems.Add(item); 
     } 
    } 
} 

** RelayCommand **(クレジットへジョシュ・スミス) https://gist.github.com/schuster-rainer/2648922

は、今のあなたはVMのコンストラクタで選択された項目のみ

<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)); 

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"; 
     } 
    } 
} 

ほら!

0

EntityCollectionViewSourceにバインドGRIDCONTROLを更新するには、それはEntityCollectionViewSourceの新しいインスタンスを作成する必要があります。このトピックについて説明したHow to work with WPF DXGrid dataサポートチケットをご覧ください。

関連する問題