DataGridView -- AllowUserToDeleteRows

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.

[855 byte] By [OferElboher] at [2008-1-4]
# 1
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

classmyDataGridView :DataGridView

{

protectedoverridebool ProcessDialogKey(Keys keyData)

{

if (keyData ==Keys.Delete)

{

this.Rows.Remove(this.CurrentRow);

returntrue;

}

returnbase.ProcessDialogKey(keyData);

}

}

Zhi-XinYe-MSFT at 2007-10-11 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 2

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.

OferElboher at 2007-10-11 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 3
Change my code above to:

Code Snippet

classmyDataGridView :DataGridView

{

protectedoverridebool ProcessDialogKey(Keys keyData)

{

if (keyData ==Keys.Delete)

{

if(this.CurrentRow.Selected )

this.Rows.Remove(this.CurrentRow);

}

returnbase.ProcessDialogKey(keyData);

}

}

Zhi-XinYe-MSFT at 2007-10-11 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 4

Thanks again (and a lot!).

I'll check it ASAP.

Sincerely,

Ofer.

OferElboher at 2007-10-11 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 5

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.

protectedoverridebool ProcessDialogKey(System.Windows.Forms.Keys keyData)

{

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

{

bool bRemovedRowInLastLoop;

do

{

bRemovedRowInLastLoop = false;

foreach (System.Windows.Forms.DataGridViewRow dgvr inthis.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);

}

returnbase.ProcessDialogKey(keyData);

}

OferElboher at 2007-10-11 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 6
Try the SelectedRows property...

Code Snippet

classmyDataGridView :DataGridView

{

protectedoverridebool ProcessDialogKey(Keys keyData)

{

if (keyData ==Keys.Delete)

{
foreach (DataGridViewRow drinthis.SelectedRows)

{

this.Rows.Remove(dr);

}

}

returnbase.ProcessDialogKey(keyData);

}

}

Zhi-XinYe-MSFT at 2007-10-11 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 7

Thanks a lot, Zhi-Xin Ye!

Ofer.

OferElboher at 2007-10-11 > top of Msdn Tech,Windows Forms,Windows Forms General...