简体   繁体   中英

DevExpress: Get filtered rows from a GridControl

this is a difficult question to explain, but I'm trying because I'm really stuck and I don't know how to solve my issue.

I've a gridControl of devExpress in my xaml. It is populated with a observableCollection in the mvvm pattern design. In this gridControl I have also a battery of filters, correctly working.

I need the content of the gridControl filtered, as the user see it. I have found a way to this, with a working sample.

But... I get always ALL the records, not only the visible ones.

I have this in the code behind of my 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();
    }
}

and this in my viewModel:

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

and this is the xaml of the gridControl

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

and finally I call it in this way:

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

it seems to work well, but, like I said before, it gets always ALL the records. So, do you have any idea? Can I post something else useful for solve the problem?

From your answer i see that you need to obtain only visible rows from grid. If so, you should use traversing grid's rows(accessing rows one by one) rather than get rows from DataController directly. To achieve this goal, you can use the GridControl.GetRowHandleByVisibleIndex() method. Please refer to the Obtaining Row Handles documentation article for more information.

The Traversing Rows article demonstrates the possible code-snippet that implements this approach.

PS The DevExpress Search Engine can get all the answers faster than I do.)

Some time ago i faced the same problem. have a look at the following links.

synchronize filtered data between the GridControl and ViewModel

example provided by Devexpress Team

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM