Modify Filtering Row Text

6 posts, 0 answers
  1. Ryan
    Ryan avatar
    35 posts
    Member since:
    Jul 2015

    Posted 31 Mar Link to this post

    Hello,

    I'm trying to cancel the RadGridView's FilterChanging event after the user enters text into the filtering row, but doing so does not undo any additional text that the user may have added to the filtering row. I have tried the following lines of code but neither worked (and I discovered the Value property does not match the RadGridView's filtering row text - it matches the actual value of the filter):

    radGridView1.FilterDescriptors[0].Value = oldValue;
    radGridView1.MasterView.TableFilteringRow.Cells[0].Value = oldValue;

    How can I set the filtering row text back to its original value?

    Thank you!

  2. Dess
    Admin
    Dess avatar
    1601 posts

    Posted 04 Apr Link to this post

    Hello Ryan,

    Thank you for writing.

    The FilterDescriptors collection contains the actual filters that are applied to RadGridView. When you cancel the FilterChanging event, the text in the filter cell is not cleared. However, the FilterDescriptor is not updated. If you need to change the text in the filter cell, you can use the ViewCellFormatting event and manipulate the CellElement.Text property for the GridFilterCellElement.

    If you are still experiencing any further difficulties, it would be greatly appreciated if you can provide additional information about the exact goal that you are trying to achieve. Thus, we would be able to think about a suitable solution and assist you further. Thank you.
     
    I hope this information helps. Should you have further questions I would be glad to help.

    Regards,
    Dess
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Ryan
    Ryan avatar
    35 posts
    Member since:
    Jul 2015

    Posted 04 Apr in reply to Dess Link to this post

    Hi Dess,

    Thanks for the response.

    I can't seem to get it to work. The CellElement.Text property of the GridFilterCellElement appears to contain the actual value of the filter, and does not match what is in the filtering cell that I want to change:

    private void radGridView1_FilterChanging(object sender, GridViewCollectionChangingEventArgs e)
    {
        if (e.NewValue != null && e.NewValue.ToString().Length > 2)
        {
            e.Cancel = true;
            undoFilter = true;
        }
    }
     
    private void radGridView1_ViewCellFormatting(object sender, CellFormattingEventArgs e)
    {
        if (undoFilter)
        {
            undoFilter = false;
            e.CellElement.Text = "test";
        }
    }

    My overall goal is to not allow the user to change records under certain circumstances. We have a data entry type form, and if a certain textbox is empty, we do not want the user to change records in the RadGridView. However, when you filter the RadGridView, after entering 2 or more characters into the filtering cell, the RadGridView automatically selects the first row. So our options would be to disable this feature or to not allow the user to continue searching. I wanted a MessageBox to appear when the user tried to change records and we didn't want them to so they knew they were doing something wrong, rather than just disabling the filtering row. Let me know if you have any questions or if any of that did not make sense.

    Thank you!

  5. Dess
    Admin
    Dess avatar
    1601 posts

    Posted 06 Apr Link to this post

    Hello Ryan,

    Thank you for writing back. 

    Note that if the filter cell is in edit mode while canceling the FilterChanging event, it is necessary to set the editor's value as well, not the cell's text:
    public Form1()
    {
        InitializeComponent();
        this.radGridView1.EnableFiltering = true;
        this.radGridView1.FilterChanging += radGridView1_FilterChanging;
     
        this.radGridView1.ViewCellFormatting += radGridView1_ViewCellFormatting;
    }
     
    private void radGridView1_ViewCellFormatting(object sender, CellFormattingEventArgs e)
    {
        GridFilterCellElement filterCell = e.CellElement as GridFilterCellElement;
        if (filterCell != null)
        {
            if (undoFilter)
            {
                undoFilter = false;
                if (this.radGridView1.IsInEditMode &&
                    e.Column == this.radGridView1.CurrentColumn &&
                    e.Row == this.radGridView1.CurrentRow &&
                    e.Row.Cells[e.Column.Name].Tag != null)
                {
                    this.radGridView1.ActiveEditor.Value = e.Row.Cells[e.Column.Name].Tag;
                }
            }
        }
    }
     
    bool undoFilter = false;
     
    private void radGridView1_FilterChanging(object sender, GridViewCollectionChangingEventArgs e)
    {
        if (e.NewValue != null && e.NewValue.ToString().Length > 2)
        {
            e.Cancel = true;
            undoFilter = true;
            this.radGridView1.MasterView.TableFilteringRow.Cells[this.radGridView1.CurrentColumn.Name].Tag = e.NewValue.ToString().Substring(0, 2);
        }
    }

    Note that this is just a sample approach and it may not cover all possible cases. Feel free to modify it in a way which suits your requirement best.

    I hope this information helps. If you have any additional questions, please let me know.

    Regards,
    Dess
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  6. Ryan
    Ryan avatar
    35 posts
    Member since:
    Jul 2015

    Posted 13 Apr in reply to Dess Link to this post

    Thanks, Dess. That seems to work.

    A couple minor change that I made involved forcing the text cursor (the SelectionStart property) to move to the end rather than the beginning of the filtering cell, and replacing the filter cell text with e.OldValue rather than a substring of e.NewValue whenever this case occurred (I'm not sure if this is the proper way to do it, but it works :)):

    public Form1()
    {
        InitializeComponent();
        this.radGridView1.EnableFiltering = true;
        this.radGridView1.FilterChanging += radGridView1_FilterChanging;
      
        this.radGridView1.ViewCellFormatting += radGridView1_ViewCellFormatting;
    }
      
    private void radGridView1_ViewCellFormatting(object sender, CellFormattingEventArgs e)
    {
        GridFilterCellElement filterCell = e.CellElement as GridFilterCellElement;
        if (filterCell != null)
        {
            if (undoFilter)
            {
                undoFilter = false;
                if (this.radGridView1.IsInEditMode &&
                    e.Column == this.radGridView1.CurrentColumn &&
                    e.Row == this.radGridView1.CurrentRow &&
                    e.Row.Cells[e.Column.Name].Tag != null)
                {
                    this.radGridView1.ActiveEditor.Value = e.Row.Cells[e.Column.Name].Tag;
                    ((Telerik.WinControls.UI.RadTextBoxEditorElement)radGridView1.CurrentCell.GetEditorElement(radGridView1.ActiveEditor)).TextBoxItem.SelectionStart = radGridView1.ActiveEditor.Value.ToString().Length;
                }
            }
        }
    }
      
    bool undoFilter = false;
      
    private void radGridView1_FilterChanging(object sender, GridViewCollectionChangingEventArgs e)
    {
        if (e.NewValue != null && e.NewValue.ToString().Length > 2)
        {
            e.Cancel = true;
            undoFilter = true;
            this.radGridView1.MasterView.TableFilteringRow.Cells[this.radGridView1.CurrentColumn.Name].Tag = e.OldValue.ToString();
        }
    }

  7. Dess
    Admin
    Dess avatar
    1601 posts

    Posted 14 Apr Link to this post

    Hello Ryan,

    Thank you for writing back. 

    I am glad that the suggested approach is suitable to your case. Manipulating the SelectionStart property is the correct approach for navigating the editor's cursor. As to the filter cell's text, feel free to use the OldValue in the FilterChanging event if it achieves the exact requirement.

    I hope this information helps. If you have any additional questions, please let me know.

    Regards,
    Dess
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Back to Top
UI for WinForms is Visual Studio 2017 Ready