DevExpress:从 GridControl 获取筛选的行

本文关键字:筛选 获取 GridControl DevExpress | 更新日期: 2023-09-27 18:31:03

这是一个很难解释的问题,但我正在尝试,因为我真的陷入困境,我不知道如何解决我的问题。

我在我的 xaml 中有一个 devExpress 的网格控制。它在 mvvm 模式设计中填充了一个可观察集合。在这个网格控制中,我还有一连串过滤器,正常工作。

我需要过滤网格控件的内容,正如用户看到的那样。我已经找到了一种方法,有一个工作样本。

但。。。我总是得到所有的记录,而不仅仅是可见的记录。

我在 xaml 背后的代码中有这个:

public interface ICustomGridControlService
{
    IList GetAllFilteredAndSortedRows();
}
public class CustomGridControlService : ServiceBase, ICustomGridControlService
{
    public static readonly DependencyProperty DataControlProperty =
        DependencyProperty.Register("DataControl", typeof(GridControl), typeof(CustomGridControlService), new PropertyMetadata(null));
    public GridControl DataControl
    {
        get { return (GridControl)GetValue(DataControlProperty); }
        set { SetValue(DataControlProperty, value); }
    }
    public IList GetAllFilteredAndSortedRows()
    {
        return DataControl.DataController.GetAllFilteredAndSortedRows();
    }
}

在我看来,这个模型:

ICustomGridControlService CustomGridControlService { get { return GetService<ICustomGridControlService>(); } }
public ICommand GetAllFilteredAndSortedRowsCommand { get; private set; }

这是网格控制的 xaml

<dxg:GridControl x:Name="dgrErog" ItemsSource="{Binding ListaMonitorErogazioni2}" SelectedItem="{Binding SelectedArchivioErogazioni}" >
    <dxg:GridControl.TotalSummary>
        <dxg:GridSummaryItem FieldName="{DynamicResource ResourceKey=Codice}" SummaryType="Count" DisplayFormat="Records: {0}"/>
        <dxg:GridSummaryItem FieldName="{DynamicResource ResourceKey=Litri}" SummaryType="Sum" DisplayFormat="Tot.: {0}"/>
        <dxg:GridSummaryItem FieldName="{DynamicResource ResourceKey=PU}" SummaryType="Average" DisplayFormat="P. medio: {0:c2}"/>
        <dxg:GridSummaryItem FieldName="{DynamicResource ResourceKey=Importo}" SummaryType="Sum" DisplayFormat="Tot.: {0}"/>
    </dxg:GridControl.TotalSummary>
    <dxg:GridControl.Columns>
        <dxg:GridColumn x:Name="ID" Binding="{Binding ID}" FieldName="{DynamicResource ResourceKey=Codice}" AllowEditing="False"/>
        <dxg:GridColumn Binding="{Binding TRAN_ID}" FieldName="N° tran." AllowEditing="False"/>
        <dxg:GridColumn Binding="{Binding IMPIANTO}" FieldName="{DynamicResource ResourceKey=Impianto}" AllowEditing="False">
            <dxg:GridColumn.EditSettings>
                <dxe:ComboBoxEditSettings 
                    NullText="" FilterCondition="Contains"
                    AllowNullInput="True"
                    ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Window},Path=DataContext.sListImpianti}"
                    IsTextEditable="True">
                </dxe:ComboBoxEditSettings>
            </dxg:GridColumn.EditSettings>
        </dxg:GridColumn>
        <dxg:GridColumn Binding="{Binding DATA_EROG}" FieldName="{DynamicResource ResourceKey=Data}" AllowEditing="False"/>
        <dxg:GridColumn Binding="{Binding IDVEI}" FieldName="{DynamicResource ResourceKey=Veicolo}" AllowEditing="False" >
            <dxg:GridColumn.EditSettings>
                <dxe:ComboBoxEditSettings 
                    NullText="" FilterCondition="Contains"
                    AllowNullInput="True"
                    ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Window},Path=DataContext.sListVeicoli}"
                    IsTextEditable="True">
                </dxe:ComboBoxEditSettings>
            </dxg:GridColumn.EditSettings>
        </dxg:GridColumn>
        <dxg:GridColumn Binding="{Binding IDTEV}" FieldName="{DynamicResource ResourceKey=TesseraV}" AllowEditing="False">
            <dxg:GridColumn.EditSettings>
                <dxe:ComboBoxEditSettings NullText="" FilterCondition="Contains" AllowNullInput="True" IsTextEditable="True"
                    ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Window},Path=DataContext.sListTessereV}"/>
            </dxg:GridColumn.EditSettings>
        </dxg:GridColumn>
        <dxg:GridColumn Binding="{Binding IDGRV}" FieldName="{DynamicResource ResourceKey=GruppoV}" AllowEditing="False">
            <dxg:GridColumn.EditSettings>
                <dxe:ComboBoxEditSettings NullText="" FilterCondition="Contains" AllowNullInput="True" IsTextEditable="True"
                    ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Window},Path=DataContext.sListGruppiV}"/>
            </dxg:GridColumn.EditSettings>
        </dxg:GridColumn>
        <dxg:GridColumn Binding="{Binding IDAUT}" FieldName="{DynamicResource ResourceKey=Autista}" AllowEditing="False">
            <dxg:GridColumn.EditSettings>
                <dxe:ComboBoxEditSettings NullText="" FilterCondition="Contains" AllowNullInput="True" IsTextEditable="True"
                    ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Window},Path=DataContext.sListAutisti}"/>
            </dxg:GridColumn.EditSettings>
        </dxg:GridColumn>
        <dxg:GridColumn Binding="{Binding IDTEA}" FieldName="{DynamicResource ResourceKey=TesseraA}" AllowEditing="False">
            <dxg:GridColumn.EditSettings>
                <dxe:ComboBoxEditSettings NullText="" FilterCondition="Contains" AllowNullInput="True" IsTextEditable="True"
                    ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Window},Path=DataContext.sListTessereA}"/>
            </dxg:GridColumn.EditSettings>
        </dxg:GridColumn>
        <dxg:GridColumn Binding="{Binding IDGRA}" FieldName="{DynamicResource ResourceKey=GruppoA}" AllowEditing="False">
            <dxg:GridColumn.EditSettings>
                <dxe:ComboBoxEditSettings NullText="" FilterCondition="Contains" AllowNullInput="True" IsTextEditable="True"
                    ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Window},Path=DataContext.sListGruppiA}"/>
            </dxg:GridColumn.EditSettings>
        </dxg:GridColumn>
        <dxg:GridColumn Binding="{Binding IDPRO}" FieldName="{DynamicResource ResourceKey=Prodotto}" AllowEditing="False">
            <dxg:GridColumn.EditSettings>
                <dxe:ComboBoxEditSettings NullText="" IsTextEditable="True" FilterCondition="Contains" AllowNullInput="True"
                    ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Window},Path=DataContext.sListProdotti}"/>
            </dxg:GridColumn.EditSettings>
        </dxg:GridColumn>
        <dxg:GridColumn Binding="{Binding IDERO}" FieldName="{DynamicResource ResourceKey=Erogatore}" AllowEditing="False"/>
        <dxg:GridColumn Binding="{Binding VOLUME_LT}" FieldName="{DynamicResource ResourceKey=Litri}" AllowEditing="False"/>
        <dxg:GridColumn Binding="{Binding KMATTUALI}" FieldName="{DynamicResource ResourceKey=Km}" AllowEditing="False"/>
        <dxg:GridColumn Binding="{Binding PRUNIT}" FieldName="{DynamicResource ResourceKey=PU}" AllowEditing="False" >
            <dxg:GridColumn.EditSettings>
                <dxe:TextEditSettings Mask="c" MaskType="Numeric" MaskCulture="eu" MaskUseAsDisplayFormat="True" />
            </dxg:GridColumn.EditSettings>
        </dxg:GridColumn>
        <dxg:GridColumn Binding="{Binding IMPORTO}" FieldName="{DynamicResource ResourceKey=Importo}" AllowEditing="False" >
            <dxg:GridColumn.EditSettings>
                <dxe:TextEditSettings Mask="c" MaskType="Numeric" MaskCulture="eu" MaskUseAsDisplayFormat="True" />
            </dxg:GridColumn.EditSettings>
        </dxg:GridColumn>
    </dxg:GridControl.Columns>
    <dxg:GridControl.View>
        <dxg:TableView AllowPerPixelScrolling="True" AllowEditing="False" ShowGroupPanel="False" ShowAutoFilterRow="True" ShowTotalSummary="True"
                ShowFilterPanelMode="ShowAlways" SearchPanelFindFilter="Contains"  />
    </dxg:GridControl.View>
</dxg:GridControl>

最后我这样称呼它:

reportMonitorErogazioni report = new reportMonitorErogazioni();
report.DataSource = CustomGridControlService.GetAllFilteredAndSortedRows();
PrintHelper.ShowPrintPreview(MainWindow.thisW, report);

似乎运行良好,但是,就像我之前说的,它总是得到所有的记录。那么,你有什么想法吗?我可以发布其他有用的东西来解决问题吗?

DevExpress:从 GridControl 获取筛选的行

从您的回答中,我看到您只需要从网格中获取可见行。如果是这样,您应该使用遍历网格的行(逐个访问行),而不是直接从DataController获取行。若要实现此目标,可以使用 GridControl.GetRowHandleByVisibleIndex() 方法。有关详细信息,请参阅获取行句柄文档一文。

遍历行一文演示了实现此方法的可能代码段。

附言DevExpress搜索引擎可以比我更快地获得所有答案。

前段时间我遇到了同样的问题。请查看以下链接。

在网格控件和视图模型之间同步筛选的数据

开发快递团队提供的示例