我想用键盘上的删除按钮删除gridview中的行。我该怎么办?
答案 0 :(得分:2)
试试这个
在form.designer.cs文件中向您的gridView添加一个委托。找到要设置gridview属性的区域,并在该部分的底部添加此行。
this.gridview.KeyDown += new System.Windows.Forms.KeyEventHandler(gridview_KeyDown);
然后在您的事件处理程序中执行以下操作
void gridview_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
//checks to see if the delete button has been clicked and if there are selected rows
if (ModifierKeys == Keys.Delete && gridview.SelectedRows.Count > 0)
{
//perform delete
}
}
答案 1 :(得分:0)
我编写此代码并删除我选择的行。
private void GridviewName_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
{
var delete = from del in Movie_List.Requests
where del.Movie_Name == GridviewName.Rows[GridviewName.CurrentCell.RowIndex].Cells["ColumnName"].Value.ToString()
select del;
foreach (var Item in delete)
{
Movie_List.Requests.DeleteOnSubmit(Item);
Movie_List.SubmitChanges();
}
}
祝你好运