Articles in this section
Category / Section

How to display a text box in a grid cell in WinForms GridControl?

3 mins read

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.

Textbox displayed in a grid

Figure 1: Textbox displayed in a Grid

Samples:

C#: DisplayTextBox

VB: DisplayTextBox

 

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 Grid Control’s feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms Grid Control 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 Grid Control and other WinForms components.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied