How to display a text box in a grid cell in WinForms GridControl?
Display a text box in grid
By default, each cell in a WinForms Grid Control is a type of textbox. To add the TextBox to the grid cell and bring it to the front of the cell, use the following method.
Solution
To add a TextBox on a cell, create an object for the TextBox class and add it to the Grid.Controls collection. In the provided sample the textbox is displayed while entering the text in the current cell.
C#
//Create the instance for textbox TextBox text = new TextBox(); public Form1() { InitializeComponent(); text.Visible = false; //Add the texbox to the grid this.gridControl1.Controls.Add(text); //Hook the event to get the textbox at the front of the grid. this.gridControl1.CurrentCellKeyPress += new KeyPressEventHandler(gridControl1_CurrentCellKeyPress); } void gridControl1_CurrentCellKeyPress(object sender, KeyPressEventArgs e) { GridCurrentCell cc = this.gridControl1.CurrentCell; //Get the co-ordinates of the current cell Rectangle rect = this.gridControl1.RangeInfoToRectangle(GridRangeInfo.Cell(cc.RowIndex, cc.ColIndex)); //Move the location of the textbox to the current cell text.Location = rect.Location; //Set the size for the textbox text.Size = rect.Size; text.Visible = true; //Set the focus to the TextBox text.Focus(); text.BringToFront(); }
VB
'Create the instance for textbox Private text As New TextBox() Public Sub New() InitializeComponent() text.Visible = False 'Add the texbox to the grid Me.gridControl1.Controls.Add(text) 'Hook the event to get the textbox at the front of the grid. AddHandler gridControl1.CurrentCellKeyPress, AddressOf gridControl1_CurrentCellKeyPress End Sub Private Sub gridControl1_CurrentCellKeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) Dim cc As GridCurrentCell = Me.gridControl1.CurrentCell 'Get the co-ordinates of the current cell Dim rect As Rectangle = Me.gridControl1.RangeInfoToRectangle(GridRangeInfo.Cell(cc.RowIndex, cc.ColIndex)) 'Move the location of the textbox to the current cell text.Location = rect.Location 'Set the size for the textbox text.Size = rect.Size text.Visible = True 'Set the focus to the TextBox text.Focus() text.BringToFront() End Sub
The following screenshot displays the textbox displayed in a Grid while entering the text in a current cell.
Figure 1: Textbox displayed in a Grid
Conclusion
I hope you enjoyed learning about how to display a text box in a grid cell in WinForms GridControl.
You can refer to our WinForms GridControl feature tour page to know about its other groundbreaking feature representations. You can also explore our documentation to understand how to create and manipulate data.
For current customers, you can check out our 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 other controls.
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!