Articles in this section
Category / Section

How to add a Pop-Up Text Box to the cell?

3 mins read

By default, text box cannot be added to the WinForms Grid Control cell. This can be achieved by creating the custom CellModel (PopUpTextBoxCellModel) and CellRenderer (PopUpTextBoxCellRenderer) from GridDropDownCellModel and GridDropDownCellRenderer.

The text box can be added to the DropDown cell by override the InitializeDropDownContainer method in custom CellRenderer(PopUpTextBoxCellRenderer).

The following are the steps that need to be followed.

Step 1: Create PopTextBoxCellModel by deriving it from GridDropDownCellModel class.

C#

public class PopUpTextBoxCellModel : GridDropDownCellModel
{
    protected PopUpTextBoxCellModel(SerializationInfo info, StreamingContext context)
    : base(info, context)
    {
    }
    public PopUpTextBoxCellModel(GridModel grid)
    : base(grid)
    {
    }
    public override GridCellRendererBase CreateRenderer(GridControlBase control)
    {
        return new PopUpTextBoxCellRenderer(control, this);
    }
}

 

VB

Public Class PopUpTextBoxCellModel
    Inherits GridDropDownCellModel
    Protected Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
        MyBase.New(info, context)
    End Sub
    Public Sub New(ByVal grid As GridModel)
        MyBase.New(grid)
    End Sub
    Public Overrides Function CreateRenderer(ByVal control As GridControlBase) As GridCellRendererBase
        Return New PopUpTextBoxCellRenderer(control, Me)
    End Function
End Class

 

Step 2: Create PopUpTextBoxCellRenderer from the GridDropDownCellRenderer.

C#

public class PopUpTextBoxCellRenderer : GridDropDownCellRenderer
{
    private TextBox tbc;
    private bool isModified = false;
    public PopUpTextBoxCellRenderer(GridControlBase grid, GridCellModelBase cellModel)
    : base(grid, cellModel)
    {
        this.DisableTextBox = true;
        DropDownButton = new GridCellComboBoxButton(this);
        this.tbc = null;
        InitializeDropDownContainer();
    }
 
    //Set the cell text for Pop-up text.
    protected override void OnInitialize(int rowIndex, int colIndex)
    {
        base.OnInitialize(rowIndex, colIndex);
        this.tbc.Text = this.Grid.Model[rowIndex, colIndex].Text;
        this.isModified = false;
    }
 
    //Set the TextBox control to DropDown.
    protected override void InitializeDropDownContainer()
    {
        base.InitializeDropDownContainer();
        tbc = new TextBox();
        tbc.Multiline = true;
        tbc.AcceptsReturn = true;
        tbc.Dock = DockStyle.Fill;
        tbc.BackColor = SystemColors.Info;
        tbc.Visible = true;
        this.tbc.TextChanged += new EventHandler(textBox_TextChanged);
        this.DropDownContainer.CloseUp += new PopupClosedEventHandler(textbox_PopupClosed);
        this.DropDownContainer.Controls.Add(tbc);
        this.DropDownContainer.Size = new Size(100, 40);
    }
 
    //This event used to set the Pop-up TextBox edited value to cell, after closing the Pop-Up TextBox.
    private void textbox_PopupClosed(object sender, PopupClosedEventArgs e)
    {
        if (tbc != null && this.isModified)
        {
            Grid.Model[this.CurrentCell.RowIndex, this.CurrentCell.ColIndex].Text = this.tbc.Text;
            this.CurrentCell.IsModified = false;
            this.isModified = false;
            this.CurrentCell.Refresh();
        }
    }
    private void textBox_TextChanged(object sender, EventArgs e)
    {
        isModified = true;
    }
}

 

VB

Public Class PopUpTextBoxCellRenderer
    Inherits GridDropDownCellRenderer
    Private tbc As TextBox
    Private isModified As Boolean = False
    Public Sub New(ByVal grid As GridControlBase, ByVal cellModel As GridCellModelBase)
        MyBase.New(grid, cellModel)
        Me.DisableTextBox = True
        DropDownButton = New GridCellComboBoxButton(Me)
        Me.tbc = Nothing
        InitializeDropDownContainer()
    End Sub
 
    'Set the cell text for Pop-up text.
    Protected Overrides Sub OnInitialize(ByVal rowIndex As Integer, ByVal colIndex As Integer)
        MyBase.OnInitialize(rowIndex, colIndex)
        Me.tbc.Text = Me.Grid.Model(rowIndex, colIndex).Text
        Me.isModified = False
    End Sub
 
    'Set the TextBox control to DropDown.
    Protected Overrides Sub InitializeDropDownContainer()
        MyBase.InitializeDropDownContainer()
        tbc = New TextBox()
        tbc.Multiline = True
        tbc.AcceptsReturn = True
        tbc.Dock = DockStyle.Fill
        tbc.BackColor = SystemColors.Info
        tbc.Visible = True
        AddHandler tbc.TextChanged, AddressOf textBox_TextChanged
        AddHandler DropDownContainer.CloseUp, AddressOf textbox_PopupClosed
        Me.DropDownContainer.Controls.Add(tbc)
        Me.DropDownContainer.Size = New Size(100, 40)
    End Sub
 
    'This event used to set the Pop-up TextBox edited value to cell, after closing the Pop-Up TextBox.
    Private Sub textbox_PopupClosed(ByVal sender As Object, ByVal e As PopupClosedEventArgs)
        If tbc IsNot Nothing AndAlso Me.isModified Then
            Grid.Model(Me.CurrentCell.RowIndex, Me.CurrentCell.ColIndex).Text = Me.tbc.Text
            Me.CurrentCell.IsModified = False
            Me.isModified = False
            Me.CurrentCell.Refresh()
        End If
    End Sub
    Private Sub textBox_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
        isModified = True
    End Sub
End Class

 

Step 3: Set the PopUpTextBoxCellModel to GridControl.

C#

this.gridControl1.Model.CellModels["PopUpTextBox"] = new PopUpTextBoxCellModel(this.gridControl1.Model);
 
this.gridControl1.ColStyles[2].CellType = "PopUpTextBox";

 

VB

Me.gridControl1.Model.CellModels("PopUpTextBox") = New PopUpTextBoxCellModel(Me.gridControl1.Model)
 
Me.gridControl1.ColStyles(2).CellType = "PopUpTextBox"

 

Screenshot

Showing add a pop-up text box in the cell

Sample Links

C#: Pop-Up TextBox CS

VB: Pop-Up TextBox VB

 

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