开发者

devexpress gridview filter solution

I have a devexpress gridview within my asp.net page which has a filter row. The built-in feature needs me to filter the from the first letters not the letters in between.

So "Sa" would find "Samuel" but not "Uncle Sam". To search using for the "Uncle Sam", I need to use %sa%. My clients don't want that.

I found a solution that requires me to use Settings-AutoFilterCondition="Contains" fo开发者_如何学Gor every field in the gridview. I have more than 20s gridview in the asp.net program.

Is there any alternatives to set the default for all gridview at once?

Thanks


I would suggest that you create a new ASP.NET theme based on the existing ones and change this property in a skin file. In this case, you will receive the required result for the whole web site without changing even a line of code :)


You can set it in the PageLoad event of the code behind like this:

[VB.NET]
Protected Sub Page_Load(sender As Object, e As EventArgs)
    For Each col As GridViewDataColumn In grid.Columns
        col.Settings.AutoFilterCondition = AutoFilterCondition.Contains
    Next
End Sub


I achieved this the following way:

  1. Added the following extension method to cut down the work, it returns all controls of a type:

    public static IEnumerable<T> GetAllControlsOfType<T>(this Control parent) where T : Control
    {
        var result = new List<T>();
        foreach (Control control in parent.Controls)
        {
            if (control is T)
            {
                result.Add((T)control);
            }
            if (control.HasControls())
            {
                result.AddRange(control.GetAllControlsOfType<T>());
            }
        }
        return result;
    }
    
  2. Add the following to my basepage OnLoad Event (which inherits from page, and all my pages inherit from), if you don't use a base page I recommend it, otherwise you could add it to each page.

       this.GetAllControlsOfType<ASPxGridView>()
        .SelectMany(gv => gv.Columns.OfType<GridViewDataTextColumn>())
        .ForEach(c => c.Settings.AutoFilterCondition = AutoFilterCondition.Contains);
    
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜