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: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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

Platform: WinForms |
Control: GridControl |
Published Date: April 30, 2011 |
Last Revised Date: May 27, 2019
Tags: celltype

By default, text box cannot be added to the DropDown 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

 

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