How to move the current cell with various options in WinForms GridGroupingControl?
Moving a current cell
By default,
the CurrentCell can be moved to a particular cell by using
the MoveTo method. This article explains about moving a
CurrentCell in the WinForms GridGroupingControl with the following
options:
- Activating the CurrentCell with cursor at the end position.
- Activating the CurrentCell with highlighting the cellvalue.
Solution
In order to
achieve this, initially the CurrentCell has to be moved to a particular cell by
using the MoveTo method. And then the cursor position and cell
value highlighting can be achieved by using the CellRenderer or GridTextBoxCellRenderer as
follows.
Activating the CurrentCell with cursor at the end position
C#
private void moveBtn_Click(object sender, EventArgs e)
{
int columnIndex = -1;
int.TryParse(this.columnIndexText.Text.ToString(), out columnIndex);
// Getting the record rowIndex based on its position.
int recordIndex = -1;
int.TryParse(this.recordIndexText.Text.ToString(), out recordIndex);
if (recordIndex > 0 && columnIndex > 0)
{
// Getting RowIndex and ColumnIndex.
columnIndex = this.gridGroupingControl1.TableDescriptor.FieldToColIndex(columnIndex - 1);
recordIndex = this.gridGroupingControl1.Table.Records[recordIndex - 1].GetRowIndex();
// Moving the current cell.
this.gridGroupingControl1.TableControl.CurrentCell.MoveTo(
recordIndex,
columnIndex,
Syncfusion.Windows.Forms.Grid.GridSetCurrentCellOptions.SetFocus
);
this.gridGroupingControl1.Focus();
// Setting the cursor at end position of cell.
if (this.gridGroupingControl1.TableControl.CurrentCell.Renderer is GridTextBoxCellRenderer)
{
GridTextBoxCellRenderer renderer =
this.gridGroupingControl1.TableControl.CurrentCell.Renderer as GridTextBoxCellRenderer;
renderer.TextBox.SelectionStart = renderer.TextBox.Text.Length;
}
}
}Private Sub moveBtn_Click(sender As Object, e As EventArgs) Handles moveBtn.Click
Dim columnIndex As Integer = -1
Integer.TryParse(Me.columnIndexText.Text.ToString(), columnIndex)
'Getting the record rowIndex based on its position.
Dim recordIndex As Integer = -1
Integer.TryParse(Me.recordIndexText.Text.ToString(), recordIndex)
If recordIndex > 0 AndAlso columnIndex > 0 Then
'Getting RowIndex and ColumnIndex.
columnIndex = Me.gridGroupingControl1.TableDescriptor.FieldToColIndex(columnIndex - 1)
recordIndex = Me.gridGroupingControl1.Table.Records(recordIndex - 1).GetRowIndex()
'Moving the current cell.
Me.gridGroupingControl1.TableControl.CurrentCell.MoveTo(
recordIndex,
columnIndex,
Syncfusion.Windows.Forms.Grid.GridSetCurrentCellOptions.SetFocus
)
Me.gridGroupingControl1.Focus()
'Setting the cursor at end position of cell.
If TypeOf Me.gridGroupingControl1.TableControl.CurrentCell.Renderer Is GridTextBoxCellRenderer Then
Dim renderer As GridTextBoxCellRenderer =
CType(Me.gridGroupingControl1.TableControl.CurrentCell.Renderer, GridTextBoxCellRenderer)
renderer.TextBox.SelectionStart = renderer.TextBox.Text.Length
End If
End If
End Sub
Figure 1:
Activating the CurrentCell with cursor at the end position
Activating
the CurrentCell by highlighting the cell value
C#
private void moveWithHighlightBtn_Click(object sender, EventArgs e)
{
int columnIndex = -1;
int.TryParse(this.columnIndexText.Text.ToString(), out columnIndex);
// Getting the record rowIndex based on its position.
int recordIndex = -1;
int.TryParse(this.recordIndexText.Text.ToString(), out recordIndex);
if (recordIndex > 0 && columnIndex > 0)
{
// Getting RowIndex and ColumnIndex.
columnIndex = this.gridGroupingControl1.TableDescriptor.FieldToColIndex(columnIndex - 1);
recordIndex = this.gridGroupingControl1.Table.Records[recordIndex - 1].GetRowIndex();
// Moving the current cell.
this.gridGroupingControl1.TableControl.CurrentCell.MoveTo(
recordIndex,
columnIndex,
Syncfusion.Windows.Forms.Grid.GridSetCurrentCellOptions.SetFocus
);
this.gridGroupingControl1.Focus();
// Highlighting the entire cell content.
if (this.gridGroupingControl1.TableControl.CurrentCell.Renderer is GridTextBoxCellRenderer)
{
GridTextBoxCellRenderer renderer =
this.gridGroupingControl1.TableControl.CurrentCell.Renderer as GridTextBoxCellRenderer;
renderer.TextBox.SelectAll();
}
}
}Private Sub moveWithHighlightBtn_Click(sender As Object, e As EventArgs) Handles moveWithHighlightBtn.Click
Dim columnIndex As Integer = -1
Integer.TryParse(Me.columnIndexText.Text.ToString(), columnIndex)
'Getting the record rowIndex based on its position.
Dim recordIndex As Integer = -1
Integer.TryParse(Me.recordIndexText.Text.ToString(), recordIndex)
If recordIndex > 0 AndAlso columnIndex > 0 Then
'Getting RowIndex and ColumnIndex.
columnIndex = Me.gridGroupingControl1.TableDescriptor.FieldToColIndex(columnIndex - 1)
recordIndex = Me.gridGroupingControl1.Table.Records(recordIndex - 1).GetRowIndex()
'Moving the current cell.
Me.gridGroupingControl1.TableControl.CurrentCell.MoveTo(
recordIndex,
columnIndex,
Syncfusion.Windows.Forms.Grid.GridSetCurrentCellOptions.SetFocus
)
Me.gridGroupingControl1.Focus()
'Highlighting the entire cell content.
If TypeOf Me.gridGroupingControl1.TableControl.CurrentCell.Renderer Is GridTextBoxCellRenderer Then
Dim renderer As GridTextBoxCellRenderer =
CType(Me.gridGroupingControl1.TableControl.CurrentCell.Renderer, GridTextBoxCellRenderer)
renderer.TextBox.SelectAll()
End If
End If
End Sub
Figure 2:
Activating the CurrentCell by highlighting the cell value
Samples:
Conclusion
I hope you
enjoyed learning about how to move the current cell with various options in WinForms GridGroupingControl.
You can refer to our WinForms GridGroupingControl’s feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms GridGroupingControl documentation to understand how to present and manipulate data.
For current customers, you can check out our WinForms components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our WinForms GridGroupingControl and other WinForms components.
If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!