)
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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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

Platform: WinForms |
Control: GridControl |
Published Date: April 19, 2015 |
Last Revised Date: December 18, 2019
Tags: styling

Display a text box in grid

By default, each cell in a GridControl 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

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon