We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Filterbar for GridGroupingControl using Text

Hi, is there a way to to implement a filterbar in a GCC which takes text entries and updates the grid upon every time a key is pressed. I saw a sample called FilterBarTextBoxGrid but i can''t get it to work. I already implemented a filterbar that takes text and i captured the keypressed event. In there i would like to get the text typed in that cell and filter the GCC by that text. Can you tell me either if there is an easier way than the one i''m taking capturing every keypress or can yoiu tell me how i can get the text out of the filterbarcell ?

7 Replies

AD Administrator Syncfusion Team April 7, 2005 04:37 PM UTC

You can try using the grid.TableControlCurrentCellValidateString event. In that event, the new text is passed as part of the event args.
private void gridGroupingControl1_TableControlCurrentCellValidateString(object sender, GridTableControlCurrentCellValidateStringEventArgs e)
{
	GridCurrentCell cc = e.TableControl.CurrentCell;
	GridTableCellStyleInfo style = e.TableControl.Model[cc.RowIndex, cc.ColIndex] as GridTableCellStyleInfo;
	if(style.TableCellIdentity.TableCellType == GridTableCellType.FilterBarCell)
	{
		Console.WriteLine(e.Inner.Text);
	}
}
In other Table events, you can try getting the current text using e.TableControl.CurrentCell.Renderer.ControlText.


AD Administrator Syncfusion Team April 8, 2005 07:45 AM UTC

Ok, but i got two problems: backspace keypresses are never catched. How do i overcome this ? if the column is set to readonly i can''t edit the filtercell. My guess would be to catch a cell click and en-/disable the readonly state of the current column and on focuslost is reset the readonly mode. Is there an easier way to go ? How can I filter for any occurence in the rows. If i enter an ''e'' I''d like to show the grid all records where an ''e'' is somewhere in the string. Thanks


AD Administrator Syncfusion Team April 8, 2005 08:14 AM UTC

I think you can catch the backspace in TableControlCurrentCellControlKeyMessage. It may be hit twice, so check the Msg to do things only once.
Keys keyCode = (Keys) ((int)e.Inner.Msg.WParam) & Keys.KeyCode;
if(e.Inner.Msg.Msg == 0x100) //WM_KEYDOWN
{
	Console.WriteLine("WM_KEYDOWN->" + keyCode.ToString());
}
else if(e.Inner.Msg.Msg == 0x101) //WM_KEYUP
{
	Console.WriteLine("WM_KEYUP->" + keyCode.ToString());
}
To handle the ReadOnly problem, you can use the QueryCellStyleInfo event. If the e.TableCellIdentity.TableCellType is a filter cell, then set e.Style.ReadOnly = false. In version 3.2, you can add custom fustions that can be used in expressions. I think adding a custom function would be the simplest way to handle this. Below is the class refernce entry on adding a custom function. /// Adds a function to the Function Library. /// /// The name of the function to be added. /// The function to be added. /// True if successfully added, False otherwise /// /// LibraryFunction is a delegate the defines the signature of functions that /// you can add to the Function Library. /// Adding a custom function requires two steps. The first is to register a name /// and LibraryFunction delegate with the ExpressionFielEvaluator object. The second step /// is to add a method to your code that implements the LibraryFunction delegate to perform /// the calculations you want done. /// /// There are restrictions on the use Functions within expressions. Functions can only be used /// stand-alone. They cannot be used as part of a more complex expression. So, /// "Func([Col1], 2*[Col2]+1)" is a valid use of a function named Func that accepts two /// arguments. But "2 * Func([Col1], 2*[Col2]+1) + 1" is not valid. If you need to use /// functions in algebraic expressions, then first add an Expression field that uses the /// function stand-alone. Then in your algebraic expression, you can refer to this Expression /// field. Argument used in library function calls, can be any algebraic combination of /// fields and constants, but they cannot contain function references. During calculations, the /// arguments are fully evaluated before being passed into the method you implement. /// /// In the sample below, /// ComputeFunc is the name of the method we add to our code to compute the function value. /// Func is the string name that we use in an expression to reference the custom function as in /// "Func([Col1], [Col2])". /// /// // step 1 - register the function name and delegate /// ExpressionFieldEvaluator evaluator = this.groupingEngine.TableDescriptor.ExpressionFieldEvaluator; //.CreateExpressionFieldEvaluator(this.groupingEngine.TableDescriptor); /// evaluator.AddFunction("Func", new ExpressionFieldEvaluator.LibraryFunction(ComputeFunc)); /// /// //. . . /// /// // step 1 - defining the method /// // Computes the absolute value of arg1-2*arg2 /// // parameter s- a list of 2 arguments /// // returns string holding computed value /// public string ComputeFunc(string s) /// { /// //get the list delimiter (for en-us, its is a comma) /// char comma = Convert.ToChar(this.gridGroupingControl1.Culture.TextInfo.ListSeparator); /// string[] ss = s.Split(comma); /// if(ss.GetLength(0) != 2) /// throw new ArgumentException("Requires 2 arguments."); /// double arg1, arg2; /// if(double.TryParse(ss[0], System.Globalization.NumberStyles.Any, null, out arg1) /// && double.TryParse(ss[1], System.Globalization.NumberStyles.Any, null, out arg2)) /// { /// return Math.Abs(arg1 - 2 * arg2).ToString(); /// } /// return ""; /// } /// /// /// '' step 1 - register the function name and delegate /// Dim evaluator As ExpressionFieldEvaluator = Me.groupingEngine.TableDescriptor.ExpressionFieldEvaluator /// evaluator.AddFunction("Func", New ExpressionFieldEvaluator.LibraryFunction(AddressOf ComputeFunc)) /// /// ''. . . /// /// '' step 1 - defining the method /// '' Computes the absolute value of arg1-2*arg2 /// '' parameter s- a list of 2 arguments /// '' returns string holding computed value /// Public Function ComputeFunc(s As String) As String /// ''get the list delimiter (for en-us, its is a comma) /// Dim comma As Char = Convert.ToChar(Me.gridGroupingControl1.Culture.TextInfo.ListSeparator) /// Dim ss As String() = s.Split(comma) /// If ss.GetLength(0) <> 2 Then /// Throw New ArgumentException("Requires 2 arguments.") /// End If /// Dim arg1, arg2 As Double /// If Double.TryParse(ss(0), System.Globalization.NumberStyles.Any, Nothing, arg1) _ /// AndAlso Double.TryParse(ss(1), System.Globalization.NumberStyles.Any, Nothing, arg2) Then /// Return Math.Abs((arg1 - 2 * arg2)).ToString() /// End If /// Return "" /// End Function ''ComputeFunc


AD Administrator Syncfusion Team April 12, 2005 08:13 AM UTC

Thanks for the input. Now my filterbarcell loses the entered text when it loses the focus. How can the text entered be persisted within the cell ?


AD Administrator Syncfusion Team April 12, 2005 05:02 PM UTC

One way you can do this is to cache the entered values somewhere (maybe in TableControlCurrentCellValidating) and then provide these values in QueryCellStyleInfo.
private Hashtable ht = new Hashtable();
private void gridGroupingControl1_QueryCellStyleInfo(object sender, GridTableCellStyleInfoEventArgs e)
{
	if(e.Style.TableCellIdentity.TableCellType == GridTableCellType.FilterBarCell)
	{
		e.Style.CellType = "TextBox";
		if( ht.ContainsKey(e.Style.TableCellIdentity.Column.MappingName))
		{
			e.Style.Text = ht[e.Style.TableCellIdentity.Column.MappingName].ToString();
		}
	}
}
private void gridGroupingControl1_TableControlCurrentCellValidating(object sender, GridTableControlCancelEventArgs e)
{
	GridCurrentCell cc = e.TableControl.CurrentCell;
	GridTableCellStyleInfoIdentity tableCellInfo = e.TableControl.Model[cc.RowIndex, cc.ColIndex].CellIdentity as GridTableCellStyleInfoIdentity;

	if(tableCellInfo.TableCellType == GridTableCellType.FilterBarCell)
	{
		string colName = tableCellInfo.Column.MappingName;
		if(ht.ContainsKey(colName))
			ht[colName] = cc.Renderer.ControlText;
		else
			ht.Add(colName, cc.Renderer.ControlText);
	}
}


AD Administrator Syncfusion Team April 13, 2005 08:25 AM UTC

ok works. Thanks. But got another problem. As soon as I type the first character in the filterbarcell a recordFilter gets set but the grid automatically jumps to first index of the entered character in the grid. So i can''t type anymore characters. Is there any property for the FilterBarCell which prevents this ? What is the problem here ?? Thank you


AD Administrator Syncfusion Team April 13, 2005 11:14 AM UTC

The problem is that in the middle of typing into a grid cell, you are trying to change the underlying datasource for the grid (which makes the grid reset things including the current cell you are typing into). This is not supported by default in our library. If you need this behavior, then you will have to work through these issues to get it. You can try calling CurrentCell.Lock before you try to apply your filter, and then call CurrentCell.Unlock after you have applied it to see if this will avoid this problem. (It may not, or it may cause other problems.) Another solution is to get the TextBox (CurrentCell.Renderer.Control), and save its SelectionStart and SelectionLength before you do the filter. Then afterwards, activate the currect cell, get the TextBox and reset SelectionStart and SelectionLength.

Loader.
Live Chat Icon For mobile
Up arrow icon