Category / Section
How to maintain the focus on filter bar cell while changing the data source?
3 mins read
Description:
As per the
Grid’s internal architecture, when you change the DataSource in a key
press event, it makes the Grid reload to ensure the current cell ends its
focus. To type continuously in the filter bar cell while
the DataSource is changing, there should be a workaround as follows.
Solution:
When you want
to maintain the focus on the filter bar cell while changing the data
source on the key press event, you need to get the location
and the text of the filter bar cell by using the TableControlCurrentCellKeyPress event. After
changing the data source, you need to move the focus to the same
filter bar cell, so that the focus is maintained on the same
cell.
C#
//Hooks the event in form load to change the data source
this.gridGroupingControl1.TableControlCurrentCellKeyPress += gridGroupingControl1_TableControlCurrentCellKeyPress;
void gridGroupingControl1_TableControlCurrentCellKeyPress(object sender, GridTableControlKeyPressEventArgs e)
{
//Gets the Current Cell renderer
GridCurrentCell cc = e.TableControl.CurrentCell;
//Checks for the Filter bar renderer
if (cc.Renderer is GridTableFilterBarExtCellRenderer)
{
int rowindex = cc.RowIndex;
int colindex = cc.ColIndex;
string text = cc.Renderer.GetDisplayText();
this.gridGroupingControl1.DataSource = null;
if (text.Length % 2 == 0)
{
this.gridGroupingControl1.DataSource = this.GetTable();
}
else
{
this.gridGroupingControl1.DataSource = this.SetTable();
for (int i = 0; i < this.gridGroupingControl1.TableDescriptor.Columns.Count; i++)
this.gridGroupingControl1.TableDescriptor.Columns[i].AllowFilter = true;
}
//Moves the Focus to the filter bar cell
e.TableControl.CurrentCell.MoveTo(rowindex, colindex, GridSetCurrentCellOptions.SetFocus);
//places the text in the renderer.
cc.Renderer.ControlText = text;
GridOriginalTextBoxControl tb = cc.Renderer.Control as GridOriginalTextBoxControl;
if (tb != null)
{
//Moves the cursor position to the end of the text.
tb.SelectionStart = tb.TextLength;
tb.SelectionLength = 0;
}
}
}
VB
'Hooks the event in form load to change the data source
Private Me.gridGroupingControl1.TableControlCurrentCellKeyPress += AddressOf gridGroupingControl1_TableControlCurrentCellKeyPress
Private Sub gridGroupingControl1_TableControlCurrentCellKeyPress(ByVal sender As Object, ByVal e As GridTableControlKeyPressEventArgs)
'Gets the Current Cell renderer
Dim cc As GridCurrentCell = e.TableControl.CurrentCell
'Checks for the Filter bar renderer
If TypeOf cc.Renderer Is GridTableFilterBarExtCellRenderer Then
Dim rowindex As Integer = cc.RowIndex
Dim colindex As Integer = cc.ColIndex
Dim text As String = cc.Renderer.GetDisplayText()
Me.gridGroupingControl1.DataSource = Nothing
If text.Length Mod 2 = 0 Then
Me.gridGroupingControl1.DataSource = Me.GetTable()
Else
Me.gridGroupingControl1.DataSource = Me.SetTable()
For i As Integer = 0 To Me.gridGroupingControl1.TableDescriptor.Columns.Count - 1
Me.gridGroupingControl1.TableDescriptor.Columns(i).AllowFilter = True
Next i
End If
'Moves the Focus to the filter bar cell
e.TableControl.CurrentCell.MoveTo(rowindex, colindex, GridSetCurrentCellOptions.SetFocus)
'places the text in the renderer.
cc.Renderer.ControlText = text
Dim tb As GridOriginalTextBoxControl = TryCast(cc.Renderer.Control, GridOriginalTextBoxControl)
If tb IsNot Nothing Then
'Moves the cursor position to the end of the text.
tb.SelectionStart = tb.TextLength
tb.SelectionLength = 0
End If
End If
End Sub