none
datagrid view, cannot delete row, changed editmode form editonkeystrokeorf2 to editonenter RRS feed

  • Question

  • hello, I am using a data grid view with combo boxes inside.  The combo boxes are bound to a list from an SQL query using table adapters, the datagrid itself is not connected to a table, so it does not edit or update anything on my database, just simply allows the user to pick a bunch of things from a series of combo boxes.

    Before I changed the 'editmode' property on my datagridview to 'editonenter' i had it on 'editonkeystrokeorf2'.  This allowed me to delete the row just by hitting the delete key. However, the problem was that I would have to click on the combo box up to three times to open it up.  Now with the editmode set to editonenter, the comboboxes open right up with one click, but I cannot delete the rows.  How can I delete the rows I want?


    ddd
    Wednesday, June 22, 2011 1:42 PM

Answers

  • Got it!

     

    Since the focus will remove from the DataGridView when you select anything in this DataGridView while we set the EditMode to "EditOnEnter", we cannot do things at that focus.

    It will enter into edit mode after you select anything in the DataGridView, so the event we can use is the EditingControlShowing event.

        private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
          e.Control.KeyDown += new KeyEventHandler(Control_KeyDown);
        }
    
        void Control_KeyDown(object sender, KeyEventArgs e)
        {
          if (e.KeyCode == Keys.Delete)
          {
            if (this.dataGridView1.SelectedRows.Count > 0)
            {
              this.dataGridView1.Rows.Remove(this.dataGridView1.SelectedRows[0]);
              e.Handled = true;
            }
          }
        }
    

    VB.NET

      Private Sub Control_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
        If ((e.KeyCode = Keys.Delete) AndAlso (Me.dataGridView1.SelectedRows.Count > 0)) Then
          Me.dataGridView1.Rows.Remove(Me.dataGridView1.SelectedRows.Item(0))
          e.Handled = True
        End If
      End Sub
    
      Private Sub DataGridView1_EditingControlShowing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
        AddHandler e.Control.KeyDown, New KeyEventHandler(AddressOf Me.Control_KeyDown)
      End Sub
    

    Best wishes,


    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, June 29, 2011 3:06 AM
    Moderator

All replies

  • This is because of the cell has been in editing model when you selected one row, so when you press the Delete key, it is did a edit operation for that cell, rather than the whole row.

     

    If there's any concern, please feel free to let us know.

    Best wishes,


    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, June 23, 2011 8:14 AM
    Moderator
  • dbansal,
    I am writing to check the status of the issue on your side. 
    What about this problem now? 
    Would you mind letting us know the result of the suggestions?
    best wishes,

    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, June 27, 2011 3:06 AM
    Moderator
  • Mike,

    You have not given me any suggestions, only reasons.  Can you please provide suggestion?


    ddd
    Monday, June 27, 2011 1:45 PM
  • If you want use the "Delete" key to delete the row, then you will need to not use this EditMode.

    When you use others EditMode, you said you would need click on the ComboBox cell three times to let it show.

    Actually, it needs only two click, first time is for the cell getting the focus, and the second time is for beginning editing and this time it will show you the edit control(ComboBox items).

    Why you need click on three times? It is because of the validated related events, focus related events and so on events will raised, they're all need time to executed the handler codes. And if you wait a second before you click on the second time on the ComboBox cell, then you will see that it only needs two times click, then you can open the ComboBox items.

    So, in this question, the solution is go back to use others EditMode, instead of change it to this("EditOnEnter").

     

    If there's any concern, please feel free to let us know.

    Best wishes,


    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, June 28, 2011 3:57 AM
    Moderator
  • Hello Mike,

    I do not want to click on the box 2 times. This defeats some purpose.  I would like to use the same edit mode.

    Can I do it like this:

     

    Private Sub DataGridView1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles

    DataGridView1.GotFocus

     

    If Control.ModifierKeys = Keys.Delete

    Then

    DataGridView1.CurrentRow.Dispose()

     

    End

    If

     

    End Sub

     

    I beleive this should work, however, I do not know the correct syntax to delete the row...

     
    ddd
    Tuesday, June 28, 2011 2:06 PM
  • Got it!

     

    Since the focus will remove from the DataGridView when you select anything in this DataGridView while we set the EditMode to "EditOnEnter", we cannot do things at that focus.

    It will enter into edit mode after you select anything in the DataGridView, so the event we can use is the EditingControlShowing event.

        private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
          e.Control.KeyDown += new KeyEventHandler(Control_KeyDown);
        }
    
        void Control_KeyDown(object sender, KeyEventArgs e)
        {
          if (e.KeyCode == Keys.Delete)
          {
            if (this.dataGridView1.SelectedRows.Count > 0)
            {
              this.dataGridView1.Rows.Remove(this.dataGridView1.SelectedRows[0]);
              e.Handled = true;
            }
          }
        }
    

    VB.NET

      Private Sub Control_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
        If ((e.KeyCode = Keys.Delete) AndAlso (Me.dataGridView1.SelectedRows.Count > 0)) Then
          Me.dataGridView1.Rows.Remove(Me.dataGridView1.SelectedRows.Item(0))
          e.Handled = True
        End If
      End Sub
    
      Private Sub DataGridView1_EditingControlShowing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
        AddHandler e.Control.KeyDown, New KeyEventHandler(AddressOf Me.Control_KeyDown)
      End Sub
    

    Best wishes,


    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, June 29, 2011 3:06 AM
    Moderator
  • dbansal,
    I am writing to check the status of the issue on your side. 
    What about this problem now? 
    Would you mind letting us know the result of the suggestions?
    best wishes,

    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, July 1, 2011 2:53 AM
    Moderator