.NET grid and delete key
Hi,
Does the .NET grid support deleting rows using the delete key?
Thanks,
Cory
0
-
Hello,
The following code demonstrates how to implement this functionality. After removing the selection, the grid selects a new row that is closest to the removal and ensures this row visible (i.e scrolls the content if necessary).grid.KeyDown += grid_KeyDown;
...
void grid_KeyDown(object sender, KeyEventArgs e)
{
if (Equals(Keys.Delete, e.KeyCode))
{
//Copy selected rows
List<Row> rows = new List<Row>(grid.Selection);
//Get an index of the first selected row
int firstSelectedRow = rows.Count > 0 ? rows[0].VisibleIndex : -1;
//Remove selection (from last to first for performance reasons)
for(int i = rows.Count - 1; i >= 0; --i)
{
rows[i].Remove();
}
//Ensure the closest row selected, focused and visible
firstSelectedRow = firstSelectedRow < grid.Rows.Count ? firstSelectedRow : grid.Rows.Count - 1;
if(firstSelectedRow >= 0)
{
grid.Rows[firstSelectedRow].Focused = true;
grid.Rows[firstSelectedRow].Selected = true;
grid.Rows[firstSelectedRow].EnsureVisible();
}
}
}Best regards,
The Dapfor Team0
Please sign in to leave a comment.
Comments
1 comment