none
Deleting a Row in DataGridView by keypress e.keychar = (char)keys.Delete RRS feed

  • Question

  • I have a datagridview which i dynamically populate the rows. After i selected a row and press the "delete" key on my keyboard I want to delete the selected row(s). Is this achievable? I tried the conditon "if (e.KeyChar == (char)Keys.Delete)" within gridview_KeyPress event but the actual pressing of the "delete" key is not caught...... please help
    thanks in advance

    Achievement provides the ultimate pleasure in life
    Friday, October 9, 2009 8:02 PM

Answers

  • You can do it by clicking Row Header and clicking delete button automatically.

    If you want to delete a row when user presses delete while editing cell content you can handle EditingControlShowing event and use the code below.

            private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
            {
                if (e.Control is TextBox) //if editcontrol is textbox
                {
                    DataGridViewTextBoxEditingControl cell = (DataGridViewTextBoxEditingControl)e.Control;
                    if (cell != null)
                    {
    
                        cell.KeyDown += delegate(object s, KeyEventArgs ekp)
                        {
            
                            if (ekp.KeyData==Keys.Delete)
                            {
                               dataGridView1.Rows.RemoveAt(cell.EditingControlRowIndex);
                            }
                        };
                    }
                }
    
            }
    • Proposed as answer by Tamer Oz Sunday, October 11, 2009 12:31 PM
    • Marked as answer by Aland LiModerator Monday, October 12, 2009 7:03 AM
    Saturday, October 10, 2009 11:24 AM

All replies

  • You can do it by clicking Row Header and clicking delete button automatically.

    If you want to delete a row when user presses delete while editing cell content you can handle EditingControlShowing event and use the code below.

            private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
            {
                if (e.Control is TextBox) //if editcontrol is textbox
                {
                    DataGridViewTextBoxEditingControl cell = (DataGridViewTextBoxEditingControl)e.Control;
                    if (cell != null)
                    {
    
                        cell.KeyDown += delegate(object s, KeyEventArgs ekp)
                        {
            
                            if (ekp.KeyData==Keys.Delete)
                            {
                               dataGridView1.Rows.RemoveAt(cell.EditingControlRowIndex);
                            }
                        };
                    }
                }
    
            }
    • Proposed as answer by Tamer Oz Sunday, October 11, 2009 12:31 PM
    • Marked as answer by Aland LiModerator Monday, October 12, 2009 7:03 AM
    Saturday, October 10, 2009 11:24 AM
  • Tamercim good job, tesekkurler..bunca yildan sonra ne guzelde ise halen yariyor
    Monday, April 22, 2019 4:56 AM