[英]How can I delete dataGridView row on keyboard delete key press ? c#
如何刪除鍵盤上的選定行刪除鍵按下(從dataGridView中刪除並從數據庫中刪除)?
我在這里填充dataGridView:
private void GetDate()
{
SqlDataAdapter adapter = new SqlDataAdapter("SELECT id as [ID],description as [Description],unit as [Unit], qty as [Quantity],unitRate as [Unit Rate], amount as [Amount], _datetime as [Date] FROM tbl_BOQ WHERE projectId = "+id, conn);
adapter.SelectCommand.CommandType = CommandType.Text;
DataTable table = new DataTable();
table.Clear();
adapter.Fill(table);
dataGridView1.DataSource = table;
}
我使用DataGridView的KeyDown事件,並在處理程序中確定它是否是按下的Delete鍵:
if e.KeyCode == Keys.Delete...
如果DataGridView在FullRowSelect或RowHeaderSelect模式下,則通過獲取SelectedRows屬性來查找要刪除的項/行,否則您可以使用以下內容確定行:
i = SelectedCells[0].RowIndex
然后:
DataGridView.Rows[i].DataBoundItem
然后你只需要從數據庫中刪除相應的記錄,並可能刷新DataGridView,取決於它如何綁定...
您是否嘗試過使用DataGridView的KeyPress事件?
然后,您可以使用DataGridView的SelectedRows屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.