0

Basicaly , this answer https://stackoverflow.com/a/2035138/8425204 solves my problem , but I do not know the C# language. Is there anything in visual basic ? I've tried the converters online but they don't solve the issue since the conversion is not exact

Vlad-Rares
  • 43
  • 9
  • I used the telerik online converter, code looks good to me. What exactly is your problem with the converted code? – jmoreno Jan 11 '19 at 12:13
  • You will need to add the appropriate Handles clause to the in order to get them called in response to the event – jmoreno Jan 11 '19 at 12:14
  • @jmoreno For example , when i try to convert the first part of the code, i receive these errrors :Error 12 function 'EditingControlWantsInputKey' cannot be declared 'Overrides' because it does not override a function in a base class. – Vlad-Rares Jan 11 '19 at 12:18
  • 1
    Did you do as the instructions say and create a class that inherits from DataGridViewTextBoxEditingControl? – jmoreno Jan 11 '19 at 12:22
  • @jmoreno Not really? I think I didn't understood the instructions.Could you please explain further? – Vlad-Rares Jan 11 '19 at 12:22
  • You need to create a new class (two different classes actually from the instructions), and inheriting from the appropriate base classes (per the instructions) and then add that code to them. You then need to set the cell template per the instructions. – jmoreno Jan 11 '19 at 12:27
  • @jmoreno "Therefore, we need to override OnKeyDown and implement the functionality ourselves, like this:" Where should I put that code ? In the aforementioned class or in the datagridview event ? – Vlad-Rares Jan 11 '19 at 12:34

1 Answers1

0

Here is the code in vb.net:
First the CustomDataGridViewTextBoxEditingControl:

Public Class CustomDataGridViewTextBoxEditingControl
    Inherits DataGridViewTextBoxEditingControl

    Public Overrides Function EditingControlWantsInputKey(keyData As Keys, dataGridViewWantsInputKey As Boolean) As Boolean
        Select Case keyData And Keys.KeyCode
            Case Keys.Enter
                Return True
            Case Else
        End Select        
        Return MyBase.EditingControlWantsInputKey(keyData, dataGridViewWantsInputKey)
    End Function

    Protected Overrides Sub OnKeyDown(e As KeyEventArgs)
        Select Case e.KeyCode And Keys.KeyCode
            Case Keys.Enter
                Dim oldSelectStart = Me.SelectionStart
                Dim CurrentText = Me.Text
                me.Text = String.Format("{0}{1}{2}",CurrentText.Substring(0,me.SelectionStart), vbCrLf,CurrentText.Substring(Me.SelectionStart + Me.SelectionLength))
                Me.SelectionStart = oldSelectStart + vbCrLf
            Case Else
        End Select
        MyBase.OnKeyDown(e)
    End Sub
End Class

Then you create CustomDataGridViewTextBoxCell:

Public Class CustomDataGridViewTextBoxCell
    Inherits DataGridViewTextBoxCell
    Public Overrides ReadOnly Property EditType As Type
        Get
            Return GetType(CustomDataGridViewTextBoxEditingControl)
        End Get
    End Property
End Class

Then define a class DataGridViewCustomColumn:

Public Class DataGridViewCustomColumn
    Inherits DataGridViewColumn

    Public Sub New()
        Me.CellTemplate = New CustomDataGridViewTextBoxCell()
    End Sub
End Class

And then in your code you write:

Dim col = new DataGridViewCustomColumn
DataGridView1.Columns.Add( col)

You have to set the properties DefaultCellStyle.WrapMode to True and AutoSizeRowMode to AllCells. This was not stated in the original answer in the C# code.

Code Pope
  • 5,075
  • 8
  • 26
  • 68
  • `dgRezervari.Columns(colIndex).CellTemplate = CustomDataGridViewTextBoxCell` . I think I may seem very dumb , sorry for that . I got an error by doing that . Which is the proper way to set it ? – Vlad-Rares Jan 11 '19 at 12:48
  • You have instantiate `CustomDataGridViewTextBoxCell`. `dgRezervari.Columns(colIndex).CellTemplate = new CustomDataGridViewTextBoxCell` – Code Pope Jan 11 '19 at 12:49
  • I've done this , but it doesn't seem to work. Now , the 'enter' key can change the cells .. – Vlad-Rares Jan 11 '19 at 13:02
  • I've seen you had edited your code, but the problem is that my DataGridView is populated from a DataTable(and then from the database) , so I can't add columns separately.Any other solution? – Vlad-Rares Jan 11 '19 at 14:14
  • @Vlad-Rares. The above code is working. So for your case, why can't you create your columns and fill it with the data of your dataset? What is the problem? In datagridview you cannot change the type of the column after you have bound it with data. The only way is to add the new column bound it with the datasource and hide the previous column. See https://stackoverflow.com/questions/40024417/c-sharp-change-existing-column-in-a-datagridview-to-a-combobox. – Code Pope Jan 11 '19 at 15:16