This is a migrated thread and some comments may be shown as answers.

Cancel a filter conditionally

1 Answer 35 Views
GridView
This is a migrated thread and some comments may be shown as answers.
anu
Top achievements
Rank 1
anu asked on 01 Feb 2011, 05:04 PM
I have a situation like this:
I need a filter for a column say ProdNumber==null, so it shows all products w/o a product number. In the grid i am enabling users to enter a product number and save. But since the filter is on as soon as the user save the record, the record disappears from the view based the filter. But the users would like to continue to see that record until they manually trigger the filter again. How can i do this?
How can i cancel the automatic filtering  and provide a button or something to trigger the filtering?
I tried to use this , but did not get fired before the automatic filtering:

 

 

void radGV_FilterChanging(object sender, Telerik.WinControls.UI.GridViewCollectionChangingEventArgs e)

 

{

e.Cancel =

 

true;

 

}

Thanks
anu

1 Answer, 1 is accepted

Sort by
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 01 Feb 2011, 10:16 PM
Hello,

Please have a look at this basic exmaple. When the user adds a row, it clears any existing filters and adds in a new one for the newly added record. The user can then re-filter manually as they need to.

Designer File
namespace RadGridView_C
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components;
  
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
  
        #region Windows Form Designer generated code
  
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.radGridView1 = new Telerik.WinControls.UI.RadGridView();
            ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).BeginInit();
            this.SuspendLayout();
            // 
            // radGridView1
            // 
            this.radGridView1.Location = new System.Drawing.Point(13, 13);
            this.radGridView1.Name = "radGridView1";
            this.radGridView1.Size = new System.Drawing.Size(341, 265);
            this.radGridView1.TabIndex = 1;
            this.radGridView1.Text = "radGridView1";
            this.radGridView1.UserAddedRow += new Telerik.WinControls.UI.GridViewRowEventHandler(this.radGridView1_UserAddedRow);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(366, 291);
            this.Controls.Add(this.radGridView1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).EndInit();
            this.ResumeLayout(false);
  
        }
  
        #endregion
  
        private Telerik.WinControls.UI.RadGridView radGridView1;
    }
}

Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls.UI;
using Telerik.WinControls.Data;
  
namespace RadGridView_C
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
  
        private void Form1_Load(object sender, EventArgs e)
        {
            this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
            this.radGridView1.EnableFiltering = true;
            this.radGridView1.ShowFilteringRow = true;
  
            GridViewTextBoxColumn prodNumberColumn = new GridViewTextBoxColumn();
            prodNumberColumn.Name = "ProdNumber";
            prodNumberColumn.HeaderText = "ProdNumber";
            prodNumberColumn.FieldName = "ProdNumber";
            this.radGridView1.Columns.Add(prodNumberColumn);
  
            GridViewTextBoxColumn prodNameColumn = new GridViewTextBoxColumn();
            prodNameColumn.Name = "ProdName";
            prodNameColumn.HeaderText = "ProdName";
            prodNameColumn.FieldName = "ProdName";
            this.radGridView1.Columns.Add(prodNameColumn);
  
            this.radGridView1.Rows.Add("Product #1", "Description for Product 1");
            this.radGridView1.Rows.Add("Product #2", "Description for Product 2");
            this.radGridView1.Rows.Add("Product #3", "Description for Product 3");
            this.radGridView1.Rows.Add(null, "Description for Product 4");
            this.radGridView1.Rows.Add(null, "Description for Product 5");
            this.radGridView1.Rows.Add(null, "Description for Product 6");
  
        }
  
        private void radGridView1_UserAddedRow(object sender, GridViewRowEventArgs e)
        {
           if (this.radGridView1.FilterDescriptors.Count > 0)
           {
               this.radGridView1.FilterDescriptors.Clear();
  
               FilterDescriptor filter = new FilterDescriptor();
               filter.Operator = FilterOperator.IsEqualTo;
               filter.Value = e.Row.Cells["ProdName"].Value;
               filter.IsFilterEditor = true;
               this.radGridView1.Columns["ProdName"].FilterDescriptor = filter;
           }
        }
  
    }
}

hope that helps
Richard
Tags
GridView
Asked by
anu
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Share this question
or