none
Allow multi-line text entry in a datagrid textbox cell, or How to capture ctrl+enter in a datagridview textbox cell

    Question

  • I want to allow a user to be able to enter multi-line text in a datagrid textbox cell,  similar to how you edit any Multi-Line textbox controls in the VS2005 IDE.

    I haven't seen any good examples of that, so my next attempt was to try to capture ctrl+enter and add a vbcrlf to the end of my text string.  My code isn't working.

    Any ideas?

    Here's the code:

        Private Sub MJ_Contact_FlagsDataGridView_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MJ_Contact_FlagsDataGridView.KeyPress

            If e.KeyChar.Equals(System.ConsoleModifiers.Control & Chr(13)) Then
                ' capture ctrl+enter and append a vbcr
                MJ_Contact_FlagsDataGridView.Rows(MJ_Contact_FlagsDataGridView.CurrentRow.Index).Cells("description").Value = MJ_Contact_FlagsDataGridView.Rows(MJ_Contact_FlagsDataGridView.CurrentRow.Index).Cells("description").Value & ControlChars.CrLf

            End If
        End Sub

    Wednesday, August 10, 2005 8:35 PM

Answers

  • You can enable Multi-line in the DataGridView by setting the column/cells WrapMode to be True.

    The grid's KeyPress event doesn't get called when the editing control is being shown because the editing control gets the keypress event instead.

    Hope this helps!

    -mark
    DataGridView Program Manager
    Microsoft
    This post is provided "as-is"
    Wednesday, September 07, 2005 8:39 PM

All replies

  • You can enable Multi-line in the DataGridView by setting the column/cells WrapMode to be True.

    The grid's KeyPress event doesn't get called when the editing control is being shown because the editing control gets the keypress event instead.

    Hope this helps!

    -mark
    DataGridView Program Manager
    Microsoft
    This post is provided "as-is"
    Wednesday, September 07, 2005 8:39 PM
  • Mark,

    I did determine tha shift+enter would create a new line when the multiline/wrapmode combo is set. 

    I would like to see the edit control expose the properties of the editing control (like acceptsreturn, acceptstab).  This would make the usage more consitant.
    Friday, September 09, 2005 12:47 PM
  • I found that if I use EditingControlShowing Event for the DataGridView
    I could capture the editing control and cast it to its appropreate type, thus
    exposing its properties.  Unfortunately the return still does not work as I would like it to.
    Shift+Enter seems to be the only way that I can enter a new line into a cell.

    using the EventHandler for DataGridView.EditingControlShowing :

    private
    void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
          ((DataGridViewTextBoxEditingControl)e.Control).AcceptsReturn = true;
    }

    Monday, November 14, 2005 7:40 PM
  • hi mark, can u help me with my problem regarding my datagridview...
    pls...
    Tuesday, June 05, 2007 1:23 PM
  •  

    Setting Multiline and AcceptReturn properties for DataGridViewTextBoxEditingControl to true works while editing, as soon as you switch to regular view mode and switch back to editing it renders end of line or return characters as empty squares, and everything is on one line. Unless wrap mode is true, then - on multiline, but end of line does not render properly.

    If you continue typing and enter end of line it would render properly the first time, until you return back to editing later.

     

    While in viewing mode DataGridViewTextBoxEditingControl renders properly when wrap mode is set to true.

     

    Is this a bug? because MSDN doc says it should behave like in a normal TextBox.  http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewtextboxeditingcontrol_properties.aspx

     

    To Microsoft:

    Is this a bug? If it is can it be addressed? If it is by design could you update MSDN docs to reflect this behaviour?

     

    Thank you,

    Monday, November 24, 2008 11:12 PM