在DevExpress GridView上过滤数据

时间:2014-01-21 13:01:19

标签: vb.net gridview webforms devexpress

我是一名MVC人员,对WebForms的经验很少,但现在我不得不为传统的VB.NET WebForms应用程序添加一些功能。

因此应用程序正在使用DevExpress网格,它在页面上显示一个非常长的网格视图,其中一列具有以下内容:

radio actions

我要求添加的额外功能是一个过滤器,用户可以说:

  

我只想看到所选择的加载单选按钮的行是Print(或其他两个动作之一)。

所以我走到了网格的底部并创建了以下内容:

filter drop down

我的想法是,用户可以点击此下拉列表并选择他或她想要过滤单选按钮操作的内容。

DevExpress GridView代码

<dx:ASPxGridView ID="GridView1" runat="server" Theme="Office2010Blue" KeyFieldName="ID" AutoGenerateColumns="False" DataSourceID="ObjectDataSource1" Width="3200px">
    <Columns>
        <!-- Many more columns go here -->
        <dx:GridViewDataColumn Caption="" VisibleIndex="29" Name="decision" Width="150px">
            <HeaderTemplate>
                <asp:LinkButton runat="server" ID="lnkPrint" OnClick="SelectPrintAll">Print All</asp:LinkButton>
                <asp:LinkButton runat="server" ID="lnkEmail" OnClick="SelectEmailAll">Email All</asp:LinkButton>
                <asp:LinkButton runat="server" ID="lnkIgnore" OnClick="SelectIgnoreAll">Ignore All</asp:LinkButton>
            </HeaderTemplate>

            <DataItemTemplate>
                <asp:UpdatePanel runat="server" ID="upRadDecision" UpdateMode="Conditional">
                    <ContentTemplate>
                        <dx:ASPxRadioButtonList ID="radDecision" runat="server" RepeatDirection="Horizontal"
                            OnSelectedIndexChanged="StoreDecisionForRow" AutoPostBack="True" Height="15px"
                            OnDataBinding="BindDecisionRadioButton">
                            <Border BorderStyle="None"></Border>
                            <Paddings Padding="0"></Paddings>
                            <Items>
                                <dx:ListEditItem Text="Print" Value="Print" />
                                <dx:ListEditItem Text="Email" Value="Email" />
                                <dx:ListEditItem Text="Ignore" Value="Ignore" />
                            </Items>
                        </dx:ASPxRadioButtonList>
                    </ContentTemplate>
                </asp:UpdatePanel>
            </DataItemTemplate>
            <Settings HeaderFilterMode="CheckedList"></Settings>
        </dx:GridViewDataColumn>
    </Columns>

    <!-- Stylres -->
    <Styles>
        <AlternatingRow Enabled="true" />
    </Styles>

    <!-- Settings -->
    <Settings ShowFilterRow="True" ShowFilterRowMenu="true" ShowFilterBar="Auto" ShowHeaderFilterButton="true" ShowGroupPanel="True" ShowFooter="True" />
    <SettingsBehavior AllowSelectByRowClick="False" />
    <SettingsBehavior AllowSelectSingleRowOnly="False" />
    <SettingsBehavior ProcessSelectionChangedOnServer="true" />
    <SettingsPager Mode="ShowAllRecords" />

    <GroupSummary>
        <dx:ASPxSummaryItem SummaryType="Count" />
    </GroupSummary>
</dx:ASPxGridView>

我在过滤器按钮中添加了一个点击处理程序,代码如下:

Private Sub btnFilterDefaults_Click(sender As Object, e As EventArgs) Handles btnFilterDefaults.Click
    Dim filterOn As String = ddDefaultsFilterOption.SelectedValue
    'Code Handling the Filtering.
    GridView1.filter
    For rowIndex As Integer = 0 To GridView1.VisibleRowCount - 1
        Dim datarow = GridView1.GetDataRow(rowIndex)
        Dim radDecision As ASPxRadioButtonList = CType(GridView1.FindRowCellTemplateControl(rowIndex, Nothing, "radDecision"), ASPxRadioButtonList)
        Dim decision As String = ""

        If radDecision.Value Is Nothing then Continue For

        decision = radDecision.Value.ToString()

        If decision.Contains(filterOn) Then
            datarow.?? <<<<< no option to hide row here!!! :/
        End If
    Next
End Sub

我希望当我抓住数据行时,我可以隐藏它,但是没有这样的选择!

2 个答案:

答案 0 :(得分:1)

我认为问题是你正在尝试将可见特征应用于数据行。你想要做的是使用GridViewRow。这是演示文稿对象。对不起我没有你的例子,here是msdn的链接

答案 1 :(得分:0)

您是否尝试将HeaderFilterMode用于GridView?我记得这个功能从12个DevExpress版本开始。请看下面的示例,如何启用它。

    <dx:GridViewDataColumn Caption="" VisibleIndex="29" Name="decision" Width="150px">
       ...
       <Settings HeaderFilterMode="CheckedList" />
   </dx:GridViewDataColumn>

如果您使用的是旧版本,或者您需要完全另外的功能,则可以为过滤器列创建自己的自定义模板。查看下面的c#上的示例

public class FilterLookupTemplate : ITemplate
{
    private const string FormatFilterValue = "FilterLookupTemplateValue_{0}";

    public string ClientIdLookup { get; private set; }
    public string FieldName { get; set; }
    public string DataSourceID { get; set; }
    public ASPxGridView GridView { get; set; }

    #region ITemplate Members

    /// <summary>
    /// Initialization template
    /// </summary>
    public void Init()
    {
        (GridView != null).Ensure<ArgumentException>("There didn't passed reference to grid view!");
        if (!GridView.IsClientSideAPIEnabled() || String.IsNullOrEmpty(GridView.ClientInstanceName))
        {
            GridView.ClientInstanceName = GridView.ID;
        }

        var column = GridView.Columns[FieldName] as GridViewDataColumn;
        (column != null).Ensure<ArgumentException>("There is error to get column by name!");
        column.Settings.ShowFilterRowMenu = DefaultBoolean.False;
        GridView.AutoFilterCellEditorCreate += OnAutoFilterCellEditorCreate;
        GridView.AutoFilterCellEditorInitialize += OnAutoFilterCellEditorInitialize;
        GridView.ProcessColumnAutoFilter += OnProcessColumnAutoFilter;
    }

    /// <summary>
    /// Creating filter dropdown control
    /// </summary>
    /// <param name="sender">Event sender</param>
    /// <param name="e">Event arguments</param>
    private void OnAutoFilterCellEditorCreate(object sender, ASPxGridViewEditorCreateEventArgs e)
    {
        if (e.Column.FieldName.Equals(FieldName))
        {
            var dde = new DropDownEditProperties { EnableClientSideAPI = true, DropDownWindowTemplate = this };
            e.EditorProperties = dde;
        }
    }

    /// <summary>
    /// Initializing filter dropdown control
    /// </summary>
    /// <param name="sender">Event sender</param>
    /// <param name="e">Event arguments</param>
    private void OnAutoFilterCellEditorInitialize(object sender, ASPxGridViewEditorEventArgs e)
    {
        if (e.Column.FieldName.Equals(FieldName))
        {
            var editor = e.Editor as ASPxDropDownEdit;
            (editor != null).Ensure<ArgumentException>("There wasn't passed reference to the drop down editor!");
            editor.ReadOnly = true
        }
    }

    /// <summary>
    /// Processing column filtering
    /// </summary>
    /// <param name="sender">Event sender</param>
    /// <param name="e">Event arguments</param>
    private void OnProcessColumnAutoFilter(object sender, ASPxGridViewAutoFilterEventArgs e)
    {
        var session = GridView.Page.Session;
        if (e.Kind == GridViewAutoFilterEventKind.CreateCriteria)
        {
            session[String.Format(FormatFilterValue, e.Column.FieldName)] = e.Value;
            if (e.Column.FieldName.Equals(FieldName) && !String.IsNullOrEmpty(e.Value))
            {
                var values = e.Value.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
                if (values.Length > 0)
                {
                    var action = new Func<string, string, FunctionOperator>((name, value) =>
                              new FunctionOperator(FunctionOperatorType.Contains, new OperandProperty(name), new OperandValue(value)));
                    if (values.Length > 1)
                    {
                        var group = new GroupOperator(GroupOperatorType.Or);
                        group.Operands.AddRange(values.Select(v => action(e.Column.FieldName, v)).ToArray());
                        e.Criteria = group;
                    }
                    else
                    {
                        e.Criteria = action(e.Column.FieldName, values[0]);
                    }
                }
            }
        }
        else
        {
            if (session[String.Format(FormatFilterValue, e.Column.FieldName)] != null)
            {
                e.Value = session[String.Format(FormatFilterValue, e.Column.FieldName)].ToString();
            }
        }
    }

    /// <summary>
    /// Rendering loolup template controls
    /// </summary>
    /// <param name="container">Container of date range template</param>
    public void InstantiateIn(Control container)
    {
        (GridView != null).Ensure<ArgumentException>("There didn't passed reference to grid view!");
        var table = new Table { Width = new Unit(200, UnitType.Pixel) };
        container.Controls.Add(table);

        var row = new TableRow();
        table.Rows.Add(row);

        var cell = new TableCell();
        row.Cells.Add(cell);

        var lbl = new ASPxLabel { ID = "lblSelect", Text = MessageResources.FilterLookupTemplate_SelectLabelText };
        cell.Controls.Add(lbl);

        cell = new TableCell();
        row.Cells.Add(cell);
        var lookup = new ASPxGridLookup
                         {
                             ID = GridView.ID + "lookupValues",
                             EnableClientSideAPI = true,
                             Width = new Unit(100, UnitType.Percentage),
                             SelectionMode = GridLookupSelectionMode.Multiple
                         };
        lookup.GridView.Width = new Unit(100, UnitType.Percentage);
        lookup.GridView.DataSourceID = DataSourceID;
        lookup.GridView.KeyFieldName = "id";
        lookup.GridView.Columns.Add(new GridViewCommandColumn { ShowSelectCheckbox = true, AllowDragDrop = DefaultBoolean.False });
        var nameColumn = new GridViewDataTextColumn { FieldName = "name" };
        nameColumn.Settings.AllowDragDrop = DefaultBoolean.False;
        nameColumn.Settings.AllowGroup = DefaultBoolean.False;
        nameColumn.Settings.AllowHeaderFilter = DefaultBoolean.False;
        nameColumn.Settings.AllowSort = DefaultBoolean.False;
        lookup.GridView.Columns.Add(nameColumn);
        lookup.EnableClientSideAPI = true;
        cell.Controls.Add(lookup);
        ClientIdLookup = lookup.ClientID;
        row = new TableRow();
        table.Rows.Add(row);
        cell = new TableCell { ColumnSpan = 2 };
        row.Cells.Add(cell);

        var lnk = new ASPxHyperLink { Text = MessageResources.FilterLookupTemplate_ApplyLinkText, NavigateUrl = "#" };
        lnk.ClientSideEvents.Click =
            String.Format("function (s, e) {{ {0}.HideDropDown(); ApplyLookupFilter({0}, {1}, '{2}', {3}); }}",
                          container.NamingContainer.NamingContainer.ClientID,
                          GridView.ClientInstanceName,
                          FieldName,
                          ClientIdLookup);
        cell.Controls.Add(lnk);
        container.Controls.Add(table);
    }

    #endregion
}

然后将其注册为您的网格视图。 ReportsCustomerDataSource是一个LINQ数据源控件。

public partial class YourPage: Page
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        new FilterLookupTemplate { FieldName = "ReportCustomers", DataSourceID = ReportsCustomerDataSource.ID, GridView = _gridView }.Init();
    }
}

在表单上看起来就像那样

enter image description here