Ofer Elboher

Hi,

Though I set my DataGridView's AllowUserToDeleteRows to true, if the user clicks at the left of a row, and then clicks delete, the row isn't deleted. It seems that the delete 'goes' to the last cell the user edited/visited in that row. Only if that cell isn't active, the row is deleted.

How can I enable row deletion, and how can the user actually do so

Thanks,

Ofer.




Re: Windows Forms General DataGridView -- AllowUserToDeleteRows

Zhi-Xin Ye - MSFT

Yes, while the current cell is in edit mode, to press Delete key will delete the content in the current cell instead of the current row you selected, if you want to delete the current row while in edit mode, you can create a class inherited from the DataGridView class, overridde its ProcessDialogKey method, use this inherited class instead of the standard DataGridView in your program.

Code Snippet

class myDataGridView : DataGridView

{

protected override bool ProcessDialogKey(Keys keyData)

{

if (keyData == Keys.Delete)

{

this.Rows.Remove(this.CurrentRow);

return true;

}

return base.ProcessDialogKey(keyData);

}

}






Re: Windows Forms General DataGridView -- AllowUserToDeleteRows

Ofer Elboher

Hi Zhi-Xin Ye,

It seems exactly what I need, but I still want the user to be able to delete typed characters in editbox cells.

So I need a way to findout whether, when delete has been pressed, the whole rows was signed by clicking on its left as said.

How can I do this

Thanks a lot for bothering,

Ofer.






Re: Windows Forms General DataGridView -- AllowUserToDeleteRows

Zhi-Xin Ye - MSFT

Change my code above to:

Code Snippet

class myDataGridView : DataGridView

{

protected override bool ProcessDialogKey(Keys keyData)

{

if (keyData == Keys.Delete)

{

if( this.CurrentRow.Selected )

this.Rows.Remove(this.CurrentRow);

}

return base.ProcessDialogKey(keyData);

}

}






Re: Windows Forms General DataGridView -- AllowUserToDeleteRows

Ofer Elboher

Thanks again (and a lot!).

I'll check it ASAP.

Sincerely,

Ofer.






Re: Windows Forms General DataGridView -- AllowUserToDeleteRows

Ofer Elboher

Hi again Zhi-Xin Ye,

I've checked your code, and it works perfectly fine.

Yet, I found 2 strange things:

1) In order to let the user select and delete a few rows at once, I couldn't use a simple 'foreach', but rather the code below (see the integrated remarks).

2) As when a few rows are removed this way the graphics shows up a bit jumpy, I tried to add "this.SuspendLayout()" in the beginning and "this.ResumeLayout()" in the end of the removal, but it didn't change anything.

Any explanation will be appreciated.

Thanks again,

Ofer.

protected override bool ProcessDialogKey(System.Windows.Forms.Keys keyData)

{

if (System.Windows.Forms.Keys.Delete == keyData)

{

bool bRemovedRowInLastLoop;

do

{

bRemovedRowInLastLoop = false;

foreach (System.Windows.Forms.DataGridViewRow dgvr in this.Rows)

{

if (dgvr.Selected)

{

// Remove first selected row, then restart this loop, until there's no row to delete.

// This may not be done in a single loop, as the removal of a selected row disrupts, for some reason, the operation of this 'foreach' loop.

this.Rows.Remove(dgvr);

bRemovedRowInLastLoop = true;

break;

}

}

}

while (bRemovedRowInLastLoop);

}

return base.ProcessDialogKey(keyData);

}






Re: Windows Forms General DataGridView -- AllowUserToDeleteRows

Zhi-Xin Ye - MSFT

Try the SelectedRows property...

Code Snippet

class myDataGridView : DataGridView

{

protected override bool ProcessDialogKey(Keys keyData)

{

if (keyData == Keys.Delete)

{
foreach (DataGridViewRow dr in this.SelectedRows)

{

this.Rows.Remove(dr);

}

}

return base.ProcessDialogKey(keyData);

}

}






Re: Windows Forms General DataGridView -- AllowUserToDeleteRows

Ofer Elboher

Thanks a lot, Zhi-Xin Ye!

Ofer.






Re: Windows Forms General DataGridView -- AllowUserToDeleteRows

panpan

take care that a user could press the delete key on a selected new row. Therefore i modified the code (here only for deleting the current row)

Code Snippet

Class MyDataGridView

Inherits DataGridView

Protected Overloads Overrides Function ProcessDialogKey(ByVal keyData As Keys) As Boolean

If keyData = Keys.Delete Then

If Not IsNothing(Me.CurrentRow) Then

If Not Me.CurrentRow.IsNewRow Then

If Me.CurrentRow.Selected Then

Me.Rows.Remove(Me.CurrentRow)

End If

End If

Return True

End If

End If

Return MyBase.ProcessDialogKey(keyData)

End Function

End Class





Re: Windows Forms General DataGridView -- AllowUserToDeleteRows

Ofer Elboher

Thanks!