datagridview button column doesn't respond to enter keystroke

vs2005 beta1

it does respond to mouse click though, but when you use enter keystroke, only leads to current row change.


Answer this question

datagridview button column doesn't respond to enter keystroke

  • muk2cl

    Thanks mark. I put the code here just in case anyone needs it.

    Public
    Class MyDatagridviewButtonColumn

    Inherits DataGridViewButtonColumn

    Public Sub New()

    Me.CellTemplate = New MyDatagridviewButtonCell

    End Sub

    End Class

     

    Public Class MyDatagridviewButtonCell

    Inherits DataGridViewButtonCell

    Public Sub New()

    MyBase.New()

    End Sub

    Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs, ByVal rowIndex As Integer)

    MyBase.OnKeyDown(e, rowIndex)

    If e.KeyCode = Keys.Enter Then

    e.Handled = True

    RaiseCellContentClick(New DataGridViewCellEventArgs(ColumnIndex, rowIndex))

    End If

    End Sub

    End Class



  • perryf

    Thanks for posting the code. Yes this is by design. We use the space bar as the "click" action for the button cell. You can customize this behavior by using inheritance - derive from the DataGridViewButtonCell and override the OnKeyDown and for the Enter key set Handled to true and raise the CellContentClick event by calling the RaiseCellContentClick method.

    Hope this helps!
    -mark
    Program Manager
    Microsoft
    This post is provided "as-is"

  • John Mark

    I can't seem to repro this. Can you provide a project or some code

    thanks!
    -mark
    Program Manager
    This post is provided "as-is"

  • adamscabana

    Public Partial Class Form1

    Inherits System.Windows.Forms.Form

    <System.Diagnostics.DebuggerNonUserCode()> _

    Public Sub New()

    MyBase.New()

    'This call is required by the Windows Form Designer.

    InitializeComponent()

    End Sub

    'Form overrides dispose to clean up the component list.

    <System.Diagnostics.DebuggerNonUserCode()> _

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

    If disposing AndAlso components IsNot Nothing Then

    components.Dispose()

    End If

    MyBase.Dispose(disposing)

    End Sub

    'Required by the Windows Form Designer

    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer

    'It can be modified using the Windows Form Designer.

    'Do not modify it using the code editor.

    <System.Diagnostics.DebuggerStepThrough()> _

    Private Sub InitializeComponent()

    Dim DataGridViewCellStyle1 As System.Windows.Forms.DataGridViewCellStyle = New System.Windows.Forms.DataGridViewCellStyle

    Me.DataGridView1 = New System.Windows.Forms.DataGridView

    Me.DataGridViewButtonColumn1 = New System.Windows.Forms.DataGridViewButtonColumn

    CType(Me.DataGridView1, System.ComponentModel.ISupportInitialize).BeginInit()

    Me.SuspendLayout()

    '

    'DataGridView1

    '

    Me.DataGridView1.Columns.Add(Me.DataGridViewButtonColumn1)

    Me.DataGridView1.Dock = System.Windows.Forms.DockStyle.Fill

    Me.DataGridView1.Location = New System.Drawing.Point(0, 0)

    Me.DataGridView1.Name = "DataGridView1"

    Me.DataGridView1.Size = New System.Drawing.Size(292, 266)

    Me.DataGridView1.TabIndex = 0

    '

    'DataGridViewButtonColumn1

    '

    DataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter

    Me.DataGridViewButtonColumn1.DefaultCellStyle = DataGridViewCellStyle1

    Me.DataGridViewButtonColumn1.HeaderText = "Column1"

    Me.DataGridViewButtonColumn1.Name = "Column1"

    '

    'Form1

    '

    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

    Me.ClientSize = New System.Drawing.Size(292, 266)

    Me.Controls.Add(Me.DataGridView1)

    Me.Name = "Form1"

    Me.Text = "Form1"

    CType(Me.DataGridView1, System.ComponentModel.ISupportInitialize).EndInit()

    Me.ResumeLayout(False)

    End Sub

    Friend WithEvents DataGridView1 As System.Windows.Forms.DataGridView

    Friend WithEvents DataGridViewButtonColumn1 As System.Windows.Forms.DataGridViewButtonColumn

    End Class





    Public
    Class Form1

    Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick

    MsgBox("clicked")

    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Me.DataGridView1.Rows.Add()

    End Sub

    End Class



  • datagridview button column doesn't respond to enter keystroke